1. 程式人生 > >java中的過濾器 --Filter

java中的過濾器 --Filter

package filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/*
 * 字元編碼過濾器
*/
public class Encodingfilter implements
Filter { //定義屬性儲存字元編碼集 String encoding; //建構函式 public Encodingfilter() { System.out.println("過濾器建構函式!"); } //銷燬的方法 public void destroy() { System.out.println("過濾器銷燬!"); } //過濾的方法 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws
IOException, ServletException { System.out.println("過濾器開始過濾!"); //中文亂碼處理 response.setContentType("text/html;charset="+encoding); response.setCharacterEncoding(encoding); request.setCharacterEncoding(encoding); //chain物件就是過濾器鏈物件 //chain物件的doFilter方法令請求往下一個過濾器傳遞
chain.doFilter(request, response); } //初始化的方法 public void init(FilterConfig fConfig) throws ServletException { System.out.println("過濾器初始化!"); //從配置檔案中讀取字元編碼集 encoding = fConfig.getInitParameter("encoding"); } }
package filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/*
 * 字元編碼過濾器
*/
public class Encodingfilter implements Filter {
    //定義屬性儲存字元編碼集
        String encoding;
        
    //建構函式
    public Encodingfilter() {
        System.out.println("過濾器建構函式!");
    }
    //銷燬的方法
    public void destroy() {
        System.out.println("過濾器銷燬!");
    }
    //過濾的方法
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("過濾器開始過濾!");
        
        //中文亂碼處理
        response.setContentType("text/html;charset="+encoding);
        response.setCharacterEncoding(encoding);
        request.setCharacterEncoding(encoding);
        
        //chain物件就是過濾器鏈物件
        //chain物件的doFilter方法令請求往下一個過濾器傳遞
        chain.doFilter(request, response);
    }

    //初始化的方法
    public void init(FilterConfig fConfig) throws ServletException {
        System.out.println("過濾器初始化!");
        
        //從配置檔案中讀取字元編碼集
        encoding = fConfig.getInitParameter("encoding");
    }

}

xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ch03</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

<context-param> <param-name>userName</param-name> <param-value>sa</param-value> </context-param> <context-param> <param-name>pwd</param-name> <param-value>888</param-value> </context-param> <!--過濾器--> <filter> <display-name>Encodingfilter</display-name> <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> </web-app>