過濾器是向 Web 應用程序的請求和響應處理添加功能的Web 服務組件。
過濾器生命周期的各個階段:
實例化->初始化--->過濾--->銷毀
•過濾器必須實現javax.servlet.Filter接口,該接口中有
–init(…)
•參數類型:FilterConfig(初始化參數對象)
–doFilter(…)
•參數有三個類型分別是:ServletRequest,ServletResponse,FilterChain
–destroy()
在SRC下創建一個filter包創建一個filterEncoding類繼承filter接口
[html] 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;
/**
* @author Administrator
*字符編碼的過濾器
*/
public class filterEncoding implements Filter {
private String requestEncoding="";
private String responseEncoding="";
public void destroy() {
// TODO Auto-generated method stub
System.out.println("字符編碼過濾器銷毀!");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// System.out.println("編碼過濾器運行中。。。");
//獲取過濾鏈
request.setCharacterEncoding(requestEncoding);
response.setContentType(responseEncoding);
//執行過濾鏈
chain.doFilter(request, response);
}
public void init(FilterConfig conif) throws ServletException {
// TODO Auto-generated method stub
System.out.println("字符編碼過濾器初始化!");
//初始化時獲取web.xml中的數據
requestEncoding=conif.getInitParameter("requestEncoding");
responseEncoding=conif.getInitParameter("responseEncoding");
}
}
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;
/**
* @author Administrator
*字符編碼的過濾器
*/
public class filterEncoding implements Filter {
private String requestEncoding="";
private String responseEncoding="";
public void destroy() {
// TODO Auto-generated method stub
System.out.println("字符編碼過濾器銷毀!");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// System.out.println("編碼過濾器運行中。。。");
//獲取過濾鏈
request.setCharacterEncoding(requestEncoding);
response.setContentType(responseEncoding);
//執行過濾鏈
chain.doFilter(request, response);
}
public void init(FilterConfig conif) throws ServletException {
// TODO Auto-generated method stub
System.out.println("字符編碼過濾器初始化!");
//初始化時獲取web.xml中的數據
requestEncoding=conif.getInitParameter("requestEncoding");
responseEncoding=conif.getInitParameter("responseEncoding");
}
}
接著在web.xml中配置
[plain] <!-- 編碼過濾器開始 -->
<filter>
<filter-name>filterEncoding</filter-name>
<filter-class>filter.filterEncoding</filter-class>
<init-param>
<param-name>requestEncoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>responseEncoding</param-name>
<param-value>text/html;charset=utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 編碼過濾器結束 --