1.新建一个Dynamic Web Project项目(勾选web.xml),里面新建5个filter文件和一个jsp文件,整体的框架如图所示: 2.ZFilter.java里面的代码如下所示: package filter;import java.io.IOException;import java.
1.新建一个Dynamic Web Project项目(勾选web.xml),里面新建5个filter文件和一个jsp文件,整体的框架如图所示:
2.ZFilter.java里面的代码如下所示:
package filter; import java.io.IOException; import java.io.PrintWriter; 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 ZFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("执行过滤器ZFilter<br>"); chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { } }
3.FirstFilter.java里面的代码如下所示:
package filter; import java.io.IOException; import java.io.PrintWriter; 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 FirstFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("首先执行过滤器<br>"); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { } }
4.SecondFilter.java里面的代码如下所示:
package filter; import java.io.IOException; import java.io.PrintWriter; 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 = "secondFilter", urlPatterns = { "/*" }) public class SecondFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("执行过滤器SecondFilter<br>"); chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { } }
5.ThreeFilter.java里面的代码如下所示:
package filter; import java.io.IOException; import java.io.PrintWriter; 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 = "threeFilter", urlPatterns = { "/*" }) public class ThreeFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("执行过滤器ThreeFilter<br>"); chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { } }
6.FourFilter.java里面的代码如下所示:
package filter; import java.io.IOException; import java.io.PrintWriter; 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 = "fourFilter", urlPatterns = { "/*" }) public class FourFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("执行过滤器FourFilter<br>"); chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { } }
7.web.xml里面的代码如下所示:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Servlet</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> <filter> <filter-name>zFilter</filter-name> <filter-class>filter.ZFilter</filter-class> </filter> <filter-mapping> <filter-name>zFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>firstFilter</filter-name> <filter-class>filter.FirstFilter</filter-class> </filter> <filter-mapping> <filter-name>firstFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
8.one.jsp里面的代码如下所示:
<%@page import="java.util.Date"%> <%@ 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>example</title> </head> <body> <h2>Test</h2> </body> </html>
9.在浏览器里面输入http://localhost:8080/Servlet/one.jsp运行之后得到的结果如下所示: