映射Filter的細節一
阿新 • • 發佈:2018-04-19
映射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>
映射Filter的細節一