1. 串联Servlet过滤器的工作流程
2. 创建两个过滤器 MyFilter1和MyFilter2
1) MyFilter1
package com.example.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 MyFilter1 */ //@WebFilter("/MyFilter1") public class MyFilter1 implements Filter { /** * Default constructor. */ public MyFilter1() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @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 System.out.println("dofilter 1"); // pass the request along the filter chain chain.doFilter(request, response); System.out.println("after dofilter 1"); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
2) MyFilter 2
package com.example.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 MyFilter2 */ //@WebFilter("/MyFilter2") public class MyFilter2 implements Filter { /** * Default constructor. */ public MyFilter2() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @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 System.out.println("dofilter 2"); // pass the request along the filter chain chain.doFilter(request, response); System.out.println("after dofilter 2"); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
3) 在web.xml 中配置过滤器
<filter> <filter-name>MyFilter1</filter-name> <filter-class>com.example.filter.MyFilter1</filter-class> </filter> <filter-mapping> <filter-name>MyFilter1</filter-name> <url-pattern>/InfoServlet</url-pattern> </filter-mapping> <filter> <filter-name>MyFilter2</filter-name> <filter-class>com.example.filter.MyFilter2</filter-class> </filter> <filter-mapping> <filter-name>MyFilter2</filter-name> <url-pattern>/InfoServlet</url-pattern> </filter-mapping>
4) 创建jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="InfoServlet"> username: <input type="text" name="username"><br> <input type="submit" value="submit"> </form> </body> </html>
5) 创建InfoServlet
package com.example.servlet; 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 InfoServlet */ @WebServlet("/InfoServlet") public class InfoServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public InfoServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getParameter("username")); request.getRequestDispatcher("infoResult.jsp").forward(request, response); } }
6) 显示结果
打印的顺序与过滤器的工作流程一致