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);
}
}
Filter中chain.doFilter(req, res)的理解
在写代码中,每次看到Filter(过滤器)中 chain.doFilter(req, res);都不懂为什么要加这一句,他的作用是什么;
代码下面也有:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ContentCachingRequestWrapper req=new ContentCachingRequestWrapper((HttpServletRequest) request);
ContentCachingResponseWrapper res=new ContentCachingResponseWrapper((HttpServletResponse) response);
long startTime = System.currentTimeMillis();
String serverUrl = req.getServletPath();
//不懂这句什么意思
chain.doFilter(req, res);
long endTime = System.currentTimeMillis();
try {
int status = res.getStatus();
String charset=res.getCharacterEncoding();
//响应体
String responsePayload = getPayLoad(res.getContentAsByteArray(),res.getCharacterEncoding());
res.copyBodyToResponse();
if(writeLogService==null) {
writeLogService = (WriteLogService) ServiceLocator.getInstance().getCtx().getBean("writeLogServiceImpl");
}
writeLogService.writeLog(startTime,endTime,serverUrl,postparams,status,responsePayload,charset);
} catch (Exception e) {
e.printStackTrace();
}
}
用法
在doFilter()方法中,在chain.doFilter()之前的代码,一般是对request执行的过滤操作;
在chain.doFilter()后面的代码,一般是对response执行的操作;
chain.doFiter()执行下一个过滤器或者业务处理器。
如果在doFilter()方法中,不写chain.doFilter(),业务无法继续往下处理;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。