Java filter中的chain.doFilter详解

转载:

一.chain.doFilter作用

1.一般filter都是一个链,web.xml 里面配置了几个就有几个。一个一个的连在一起
request -> filter1 -> filter2 ->filter3 -> …. -> request resource.

2.chain.doFilter将请求转发给过滤器链下一个filter , 如果没有filter那就是你请求的资源

二.举例

  1. input.jsp 是用来提交输入的 : 当提交后,过滤器检测姓名和年龄 .
  2. 如果正常的话会提交给output.jsp 如果不正常提交给erroroutput.jsp.
  3. 在此 , 同时也有一个过滤器,防止乱码问题存在 , 该过滤器检测一个页面是否设置了字符编码,如果没有则进行设置。

1.input.jsp

 

<form action="output.jsp" name="form" method="post">
<table>
<tr>
<td>name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>age</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td><input type="submit" name="ok" value="ok"/></td>
</tr>
</table>
</form>

2.web.xml

<filter>
<description></description>
<display-name>encodefilter</display-name>
<filter-name>encodefilter</filter-name>
<filter-class>servletbean.encodefilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodefilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter>
<description></description>
<display-name>myfilter</display-name>
<filter-name>myfilter</filter-name>
<filter-class>servletbean.myfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/output.jsp</url-pattern>
</filter-mapping>

3.encodefilter.java

package servletbean;
public class encodefilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if(request.getCharacterEncoding()==null) {
System.out.println(encoding);
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);//到下一个链
}
public void init(FilterConfig fConfig) throws ServletException {
this.config=fConfig;
encoding=fConfig.getInitParameter("encoding");//获得目标编码格式
}
}

3.myfilter.java

package servletbean;
import javax.swing.JOptionPane; public class myfilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
response.setContentType("text/html");
response.setCharacterEncoding("GB2312"); PrintWriter out=response.getWriter();
String name="";
String age="";
int age1; name=request.getParameter("name");
age=request.getParameter("age");
RequestDispatcher dispatch=request.getRequestDispatcher("erroroutput.jsp");
if(name==null||name==""||name==" "||age==null) {
JOptionPane.showMessageDialog(null,"用户名和年龄输入错误!");
dispatch.forward(request, response);
return;
}
else{
try {
age1=Integer.parseInt(age);
}catch(Exception e){
//JOptionPane.showMessageDialog(null,"年龄必须为数字!");
dispatch.forward(request,response);
return;//如果是错误页面就到erroroutput.jsp中
}
}
//这里表示是正确的,也就是说,他回去找下一个链,但是它下面已经没有了,所以就会去跳转页面了,此跳转的页面就是action="output.jsp"了
chain.doFilter(request, response);
}
}

转载地址:http://blog.csdn.net/haofengjiao/article/details/70186381

上一篇:最实用的golang调试bug 以及性能问题的实践方法


下一篇:WICED -- Wireless Internet Connectivity for Embedded Devices