继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

映射Filter的细节一

android零基础入门
关注TA
已关注
手记 288
粉丝 97
获赞 603

在默认情况下,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>

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP