过滤指定servlet的顺序

过滤指定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());
	}

}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕小尤 2020-03-26 10:09:10

同学你好,过滤器的顺序由 web.xml 文件中 <filter-mapping>的顺序决定,从上到下的顺序来执行的。同学在filter-mapping设置地址(url-pattern)即可,可通过设置的地址进行过滤。不需要设置servlet-name。修改后代码如下所示:

http://img1.sycdn.imooc.com//climg/5e7c50d609c4ae2b08210464.jpg

如果解答了同学的疑问,望采纳~祝学习愉快~

  • 提问者 weixin_慕勒8187086 #1
    你好,和输出语句放在doFilter前后没有关系。 doFilter前面有输出到页面的语句。 你看xml的配置,filter-mapping 里的filter顺序是 firstFilter, charertfilter, annotationfilter,即AAA, bbb,ccc 。第一个firstfilter 是过滤指定的Servlet,执行顺序就变成了bbb ccc aaa,如果不过滤指定servlet 则是按照filter-mapping的顺序执行。可以看我之前回复的贴图
    2020-03-26 11:18:09
  • 同学你好,是老师理解错误,已修改上次回答,请同学查看,给您的学习带来不便,深感抱歉!祝学习愉快!
    2020-03-26 14:56:39
提问者 weixin_慕勒8187086 2020-03-26 00:19:42

忘记贴图了

http://img1.sycdn.imooc.com//climg/5e7b8499088fc6d209640209.jpg

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师