java web過濾器

(一)編寫一個過濾器改變請求編碼
【步驟1】編寫一個loginform.html文件

<html>
  <head>
    <title>使用過濾器改變請求編碼</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  </head>
  <body>
  <center>
  <h2>請輸入用戶名和口令:</h2>
  <form method="post" action="servlet/CheckParamServlet">
	<table>
	  <tr>
		<td>用戶名:</td>
		<td><input name="name" type="text"></td>
	  </tr>
	  <tr>
		<td>口    令:</td>
		<td><input name="pass" type="password"></td>
	  </tr>
	  <tr>
	        <td></td>
                <td>
                  <input name="ok" type="submit" value="提交">
                  <input name="cancel" type="reset" value="重置">
                </td>
	  </tr>
	</table>
  </form>
  </center>
 </body>
</html>

【步驟2】編寫處理請求參數的Servlet

public class CheckParamServlet extends HttpServlet{
   public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {

     String name = request.getParameter("name");
     String pass = request.getParameter("pass");
     response.setContentType("text/html;charset=gb2312");
     PrintWriter out = response.getWriter();
     
     out.println("<html><head><title>Param Test</title></head>");   
     out.println("<h3 align=center>你的用戶名爲:"+name+"</h3>");
     out.println("<h3 align=center>你的口令爲:"+pass+"</h3>");
     out.println("</body></html>");          
   }
   
   public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {
       
       doGet(request,response);
   } 
}

【步驟3】修改web.xml文件

<servlet>
     <servlet-name>CheckParamServlet</servlet-name>
     <servlet-class>CheckParamServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>CheckParamServlet</servlet-name>
     <url-pattern>/servlet/check</url-pattern>
  </servlet-mapping>

【步驟4】在瀏覽器的地址欄中輸入下面URL:
http://localhost:8080/ helloapp/loginform.html
在這裏插入圖片描述
然後點擊“提交”按鈕,經CheckParamServlet處理後返回的結果如下圖所示:
在這裏插入圖片描述
從這裏我們可以看到,從服務器返回的漢字成了亂碼。原因是沒有指定request的編碼。
下面通過編寫一個過濾器改變請求編碼。

public class EncodingFilter implements Filter {
	protected String encoding = null;
	protected FilterConfig config;
	public void init(FilterConfig filterConfig) throws ServletException {
		this.config = filterConfig;
		// 得到在web.xml中配置的編碼
		this.encoding = filterConfig.getInitParameter("Encoding");
	}
	public void doFilter(
		ServletRequest request,
		ServletResponse response,
		FilterChain chain)
		throws IOException, ServletException {
		if (request.getCharacterEncoding() == null) {			
			// 得到指定的編碼
			String encode = getEncoding();
			if (encode != null) {				
				//設置request的編碼
				request.setCharacterEncoding(encode);
				response.setCharacterEncoding(encode);		
			}
		}
		chain.doFilter(request, response);
	}
	protected String getEncoding() {
		return encoding;
	}
	public void destroy() {
	}
}

【步驟6】在web.xml文件中配置過濾器,加入下面代碼:

<filter>
   <filter-name>EncodingFilter</filter-name>
   <filter-class>filter.EncodingFilter</filter-class>
      <init-param>
        <param-name>Encoding</param-name>
        <param-value>utf-8</param-value>
      </init-param>
 </filter>
  <filter-mapping>
   <filter-name>EncodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

【步驟7】重複第(4)步操作,結果如下:

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章