过滤指定servlet的顺序
为什么过滤指定servlet的filter不是按filter-mapping里的先后顺序执行? 第一个filter是过滤指定servlet,但是是最后才被执行?
xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Filter</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>testservlet</servlet-name> <servlet-class>pers.hyu.filter.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>testservlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> <filter> <filter-name>first_filter</filter-name> <filter-class>pers.hyu.filter.FirstFilter</filter-class> </filter> <filter-mapping> <filter-name>first_filter</filter-name> <servlet-name>testservlet</servlet-name> <!-- <url-pattern>*.jsp</url-pattern> <url-pattern>*.html</url-pattern> --> </filter-mapping> <filter> <filter-name>charset</filter-name> <filter-class>pers.hyu.filter.CharertFilter</filter-class> <init-param> <param-name>charertSet</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>charset</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>annotatoin</filter-name> <filter-class>pers.hyu.filter.AnnotatoinFilter</filter-class> </filter> <filter-mapping> <filter-name>annotatoin</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3个filter
package pers.hyu.filter; 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; import javax.servlet.annotation.WebFilter; //@WebFilter(filterName = "filterF", urlPatterns="/*") public class FirstFilter implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub response.getWriter().println("AAA----This is the first filter!!!"); chain.doFilter(request, response); System.out.println("AAAAAA!"); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
package pers.hyu.filter; 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; import javax.servlet.annotation.WebFilter; //@WebFilter(filterName = "filterC",urlPatterns="/*") public class CharertFilter implements Filter { private String c; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub response.setContentType("text/html;charset=" + c); //System.out.println(c); response.getWriter().println("BBBB ----- This is the charet filter!!!"); chain.doFilter(request, response); System.out.println("BBB"); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub c = arg0.getInitParameter("charertSet"); } }
package pers.hyu.filter; 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; import javax.servlet.annotation.WebFilter; /** * Servlet Filter implementation class AnnotatoinFilter */ //@WebFilter(filterName = "filterA",urlPatterns="/*") public class AnnotatoinFilter implements Filter { /** * Default constructor. */ public AnnotatoinFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub //System.out.println("Destory "); } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here response.getWriter().println("CCC ---- This is the annotation filter!!!"); // pass the request along the filter chain chain.doFilter(request, response); System.out.println("ccc"); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub //System.out.println("Init "); } }
要过滤的servlet
package pers.hyu.filter; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestServlet */ //@WebServlet("/test") public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TestServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } }
25
收起
正在回答 回答被采纳积分+1
2回答
weixin_慕勒8187086
2020-03-26 00:19:42
忘记贴图了
相似问题
登录后可查看更多问答,登录/注册
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星