<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>HelloServlet.java
package com.zero; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "HelloServlet", urlPatterns = { "/helloServlet","/worldServlet" }, loadOnStartup = -1, initParams = { @WebInitParam(name = "key", value = "value") }) public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("doGet"); ; PrintWriter writer = resp.getWriter(); writer.write("<p1>Hello<p1>"); writer.flush(); writer.close(); } @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("HelloServlet init"); } }HelloListener.java
package com.zero; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener("HelloListener") public class HelloListener implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("HelloListener contextDestroyed"); } @Override public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub System.out.println("HelloListener contextInitialized"); } }CharFilter.java
package com.zero; 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; import javax.servlet.annotation.WebInitParam; @WebFilter(filterName="CharFilter",urlPatterns = { "/*" }, initParams = { @WebInitParam(name = "key", value = "value") }) public class CharFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub System.out.println("CharFilter destroy"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("CharFilter..."); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub System.out.println("CharFilter init"); } }OtherFilter.java
package com.zero; 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; import javax.servlet.annotation.WebInitParam; @WebFilter(filterName = "OtherFilter", urlPatterns = { "/*" }, initParams = { @WebInitParam(name = "key", value = "value") }) public class OtherFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub System.out.println("OtherFilter destroy"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("OtherFilter..."); chain.doFilter(request, response); } @Override public void init(FilterConfig conf) throws ServletException { System.out.println("OtherFilter init"); } }浏览器访问http://localhost:8080/servlettest/helloServlet
HelloListener contextInitialized CharFilter init OtherFilter init HelloServlet init CharFilter... OtherFilter... doGetPS:之前控制多个filter的执行顺序是通过web.xml中控制filter的位置顺序来控制的。如果通过注解方式,想要控制filer的执行顺序可以 通过控制filter的文件名 来控制。比如:两个文件的 首字母A排B之前 ,导致每次执行的时候都是先执行A开头的filter再执行B开头的filter。