首页 > 代码库 > 对chain.doFilter(request,response)的理解

对chain.doFilter(request,response)的理解

他的作用是将请求转发给过滤器链上下一个对象。这里的“下”指的是哪里 ?

指的是下一个filter,如果没有filter那就是你请求的资源。

 

一般filter都是一个链,web.xml 里面配置了几个就有几个。一个一个的连在一起 

request -> filter1 -> filter2 ->filter3 -> .... -> request resource.

下面举一个例子:
 
input.jsp 是用来提交输入的,当提交后,过滤器检测姓名和年龄,如果整常的话会提交给output.jsp,如果不正常提交给erroroutput.jsp.在此 同时也有一个过滤器,它是用来检测一个页面是否设置了字符编码,如果没有则进行设置。(防止乱码问题存在)。
==================
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");
}
 
}
==============
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="http://www.mamicode.com/ok"/>
</td>
</tr>
</table>
</form>
 
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中
}
}
chain.doFilter(request, response);//这里表示是正确的,也就是说,他回去找下一个链,但是它下面已经没有了,所以就会去跳转页面了,此时要跳转的页面就是action="output.jsp"了。
}
}
================
web.xml
===============
注意:filter是有顺序的
<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>

对chain.doFilter(request,response)的理解