JSP过滤器的应用 编码过滤器
过滤器是向 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>
<!-- 编码过滤器结束 --
上一篇: 转发和重定向的区别