在默认情况下,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 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦