为了账号安全,请及时绑定邮箱和手机立即绑定

映射Filter的细节一

标签:
Java

在默认情况下,Filter只过滤Request的请求,即web资源之间的转包和包含不过滤

MappingFilter::doFilter():A
FromServlet::doGet()
ToServlet::doGet()
MappingFilter::doFilter():B

FromServlet

import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class FromServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        System.out.println("FromServlet::doGet()");        request.getRequestDispatcher("/ToServlet").forward(request,response);        //request.getRequestDispatcher("/ToServlet").include(request,response);    }}

ToServlet

import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ToServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        System.out.println("ToServlet::doGet()");        //int i = 10 / 0;    }}

MappingFilter

import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class MappingFilter implements Filter {    public void destroy() {    }    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {        System.out.println("MappingFilter::doFilter():A");        chain.doFilter(request,response);        System.out.println("MappingFilter::doFilter():B");    }    public void init(FilterConfig filterConfig) throws ServletException {    }}

web.xml

<filter>    <filter-name>MappingFilter</filter-name>    <filter-class>cn.web.filter.MappingFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>MappingFilter</filter-name>    <url-pattern>/FromServlet</url-pattern>  </filter-mapping>  <filter-mapping>    <filter-name>MappingFilter</filter-name>    <url-pattern>/ToServlet</url-pattern>  </filter-mapping>

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消