手记

映射Filter的细节一

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

0人推荐
随时随地看视频
慕课网APP