`

chain.doFilter(request,response)

阅读更多
这句话的真正意思,我到现在才体会出来。
他的作用是将请求转发给过滤器链上下一个对象。这里的“下”指的是哪里 ?
值得是下一个filter,如果没有filter那就是你请求的资源。 
一般filter都是一个链,web.xml 里面配置了几个就有几个。一个一个的连在一起 
request -> filter1 -> filter2 ->filter3 -> .... -> request resource.

下面举一个例子:
input.jsp是用来提交输入的,当提交后,过滤器检测姓名和年龄,如果整常的话会提交给output.jsp,如果不正常提交给erroroutput.jsp.在此同时也有一个过滤器,它是用来检测一个页面是否设置了字符编码,如果没有则进行设置。(防止乱码问题存在)。
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"了。
}
}
==================
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>
================
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>

分享到:
评论

相关推荐

    java中五种常用的过滤器

    response.sendRedirect(request.getContextPath() + redirectURL); } else { filterChain.doFilter(request, response); } } } // 省略destroy和init方法 } ``` 三、字符编码过滤器 字符编码过滤器`...

    聊天室程序下载

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (encoding != null) { request.setCharacterEncoding(encoding); ...

    javaweb拦截器配置及原理.doc

    response.setCharacterEncoding(encoding); chain.doFilter(request, response); } public void destroy() { // nothing to do } } ``` Java Web 拦截器是一种强大且灵活的技术,允许开发者对 Web 服务器管理...

    登录过滤器

    response.sendRedirect(request.getContextPath()); }else{ chain.doFilter(request, response); } } web.xml配置: &lt;filter-name&gt;accessFilter &lt;filter-class&gt;com.loginFilter&lt;/filter-class&gt; ...

    JavaWeb开发几个常用的过滤器源码

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse httpResponse = (HttpServletResponse) response; ...

    Filter过滤器的概念和使用

    chain.doFilter(request, response); // 将请求继续传递给下一个Filter或目标资源 } ``` 这段代码展示了如何通过Filter设置字符编码。首先尝试从Filter配置中获取字符集参数,如果没有则使用默认值。接着设置请求...

    过滤器链的一个小Demo

    3. `doFilter()`方法内部,首先调用`chain.doFilter(request, response)`将请求传递给下一个过滤器,直到链末尾。 4. 当请求到达链末尾的过滤器时,这个过滤器不再调用`chain.doFilter()`,而是直接或间接地将请求...

    过滤器简单使用代码!!

    response.setCharacterEncoding("UTF-8"); // 设置响应编码 chain.doFilter(request, response); // 继续传递请求到下一个过滤器或目标资源 } } ``` 2. **检查字符**: 过滤器也可以用于检查请求参数中的特殊...

    compress压缩网站

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest && response instanceof ...

    使用HttpServletRequestWrapper在filter修改request参数

    在`doFilter`方法中,我们可以使用`chain.doFilter(request, response)`前,将原始请求包装成`CustomRequestParamWrapper`实例: ```java public void doFilter(ServletRequest request, ServletResponse response,...

    在使用实现过滤器进行request包装,获取内层request的分析

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; ...

    Struts2-forbid-getrequest.zip_DEMO

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; if ("GET"....

    Java-filter过滤器在项目开发中的应用.docx

    response.setCharacterEncoding("utf-8"); chain.doFilter(request, response); } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } } ``` 在web.xml...

    JSE基础与设计模式

    public void doFilter(Request request, Response response, FilterChain chain) { if (index == filters.size()) return; Filter filter = filters.get(index); index++; filter.doFilter(request, response...

    JSE基础与设计模式实例

    public void doFilter(Request request, Response response, FilterChain chain) { if (index == filters.size()) return; Filter filter = filters.get(index); index++; filter.doFilter(request, response...

    servlet过滤器Api 以及乱码处理.javascript注入问题

    chain.doFilter(request, response); } ``` 这段代码确保了请求和响应都使用UTF-8编码,防止了可能出现的乱码。 JavaScript注入是一种常见的安全威胁,攻击者可能通过在用户输入的数据中嵌入恶意脚本来执行任意的...

    j2ee过滤器Filter使用详解(实例)

    2. `doFilter(ServletRequest request, ServletResponse response, FilterChain chain)`:这是过滤器的核心方法,它负责处理请求和响应。当一个请求到达时,`doFilter`会被调用,通过`FilterChain`对象将请求传递给...

    Filter(过滤器)简介和工作原理

    在这个方法中,开发者可以通过request和response对象获取和修改请求或响应的数据,并通过调用`chain.doFilter(request, response)`将请求传递给下一个过滤器或者目标资源。 3. `void destroy()`: 过滤器的销毁方法,...

    response in servlet

    2. 过滤(doFilter):每次请求匹配到Filter配置时,都会调用`doFilter(ServletRequest request, ServletResponse response, FilterChain chain)`方法。在这里,我们可以获取并处理Response信息。 3. 销毁(destroy...

    filter过滤器流程及中文乱码解决和客户端IP地址控制

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; ...

Global site tag (gtag.js) - Google Analytics