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

指的是下一个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="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)的理解的更多相关文章

  1. 过滤器中的chain.doFilter(request,response)

    Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码.做一些业务逻辑判断等.其工作原理是,只要你在web.xml文件配置好要 ...

  2. chain.doFilter(request,response)含义

    过滤器的生命周期一般都要经过下面三个阶段: 初始化 当容器第一次加载该过滤器时,init() 方法将被调用.该类在这个方法中包含了一个指向 Filter Config 对象的引用.我们的过滤器实际上并 ...

  3. 过滤器chain.doFilter(request,response)的含义

    过滤器的生命周期一般都要经过下面三个阶段: 初始化: 当容器第一次加载该过滤器时,init() 方法将被调用.该类在这个方法中包含了一个指向 Filter Config 对象的引用.我们的过滤器实际上 ...

  4. //可以不保存在session中, 并且前面我保存在request,这里session也可以获取 chain.doFilter(request, response); //只有登录名不为空时放行,防止直接登录 成功的页面

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...

  5. 过滤器链chain.doFilter(request,response)含义

    过滤器的生命周期一般都要经过下面三个阶段: 初始化 当容器第一次加载该过滤器时,init() 方法将被调用.该类在这个方法中包含了一个指向 Filter Config 对象的引用. 过滤 过滤器的大多 ...

  6. Java filter中的chain.doFilter详解

    转载: 一.chain.doFilter作用 1.一般filter都是一个链,web.xml 里面配置了几个就有几个.一个一个的连在一起 request -> filter1 -> fil ...

  7. 使用filter导致服务器返回的页面始终是空白---在doFilter中漏写了chain.doFilter()

    今天调代码的时候,突然发现,服务器开着,什么都没有问题,当我把下面这个filter给deploy了以后,访问所有的页面就都是空白. 后来发现,是因为在代码路径中,有一条路径没有调用filterChai ...

  8. track message forwards, avoiding request loops, and identifying the protocol capabilities of all senders along the request/response chain

    https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html The TRACE method is used to invoke a remote, ...

  9. java 过滤器Filter中chain.doFilter()之前和之后代码的执行顺序

    过滤器拦截到响应url的请求后会先执行doFilter()方法中chain.doFilter()之前的代码,然后执行下一个过滤器或者servelt.紧接着执行chain.doFilter()之后的代码 ...

随机推荐

  1. VS报错:_CRT_SECURE_NO_WARNINGS

    常见报错:warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead ...

  2. PHP的学习--使用PhpDocumentor 2生成API文档

    官网地址:http://www.phpdoc.org/ 项目地址:https://github.com/phpDocumentor/phpDocumentor2 phpDocumentor 2是一个可 ...

  3. tomcat架构之-----基本概念

    一直都没有搞明白tomcat中server.service.Engine.Host.Context概念的意义,最近认真看了<Tomcat 6 Developer Guide>,有了进一步的 ...

  4. Testing - 测试基础 - 方法

    选择和使用测试方法和工具 按照测试需求用途(或测试技巧)选择 在软件开发生命周期和软件测试流程中适当地选择 按照测试人员实际技能选择 选择可提供的和可执行的 测试方法 类别及技巧 目标 使用方法 举例 ...

  5. 【Android】YUV使用总结 —— Android常用的几种格式:NV21/NV12/YV12/YUV420P的区别

    工作问题接触到图像这一块,需要对手机摄像头采集的原始帧做Rotate或者scale,但无奈对此的了解少之又少,于是网上搜了一顿,完事后将最近所学总结一下,以方便之后的人别踩太多坑.       首先想 ...

  6. jQuery邮箱自动补全代码

    JScript 代码   复制 (function($){ $.fn.emailMatch= function(settings){ var defaultSettings = { emailTip: ...

  7. [New Portal]Windows Azure Virtual Machine (22) 使用Azure PowerShell,设置Virtual Machine Endpoint

    <Windows Azure Platform 系列文章目录> 我们可以通过Windows Azure Management Portal,打开Virtual Machine的Endpoi ...

  8. Azure Automation (1) 入门

    <Windows Azure Platform 系列文章目录> 通过Azure Automation(自动化),开发人员可以自动完成通常要在云环境中执行的手动.长时间进行.易出错且重复性高 ...

  9. 【Pig源码分析】谈谈Pig的数据模型

    1. 数据模型 Schema Pig Latin表达式操作的是relation,FILTER.FOREACH.GROUP.SPLIT等关系操作符所操作的relation就是bag,bag为tuple的 ...

  10. 怎么让composer加速

    composer install 为什么这么慢? 下面是一个composer install(在没有composer cache的情况下)做的所有事情: [vagrant@localhost comp ...