java Filter的简单使用
java web中的过滤器的简单使用、直接上代码。
1、web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 version="3.0"> 6 <display-name>testFilter</display-name> 7 8 <filter> 9 <filter-name>filter1</filter-name> 10 <filter-class>test_servlet_package.SimpleFilter1</filter-class> 11 </filter> 12 <filter-mapping> 13 <filter-name>filter1</filter-name> 14 <url-pattern>/*</url-pattern> 15 </filter-mapping> 16 17 <filter> 18 <filter-name>filter2</filter-name> 19 <filter-class>test_servlet_package.SimpleFilter2</filter-class> 20 </filter> 21 <filter-mapping> 22 <filter-name>filter2</filter-name> 23 <url-pattern>/*</url-pattern> 24 </filter-mapping> 25 26 <servlet> 27 <servlet-name>testServlet</servlet-name> 28 <servlet-class>test_servlet_package.test_servlet</servlet-class> 29 <init-param> 30 <param-name>test</param-name> 31 <param-value>test1</param-value> 32 </init-param> 33 <load-on-startup>1</load-on-startup> 34 </servlet> 35 <servlet-mapping> 36 <servlet-name>testServlet</servlet-name> 37 <url-pattern>/testServlet</url-pattern> 38 </servlet-mapping> 39 </web-app>
2、test_servlet.java、SimpleFilter2.java、SimpleFilter1.java
1 package test_servlet_package; 2 import java.io.IOException; 3 import javax.servlet.Filter; 4 import javax.servlet.FilterChain; 5 import javax.servlet.FilterConfig; 6 import javax.servlet.ServletException; 7 import javax.servlet.ServletRequest; 8 import javax.servlet.ServletResponse; 9 public class SimpleFilter1 implements Filter { 10 // 随着容器 一起 起来 。。。初始化。。。 11 @SuppressWarnings("unused") 12 private FilterConfig filterConfig; 13 public void init(FilterConfig config) throws ServletException { 14 this.filterConfig = config; 15 } 16 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { 17 try { 18 System.out.println("Within SimpleFilter1:Filtering the Request..."); 19 chain.doFilter(request, response);// 把处理发送到下一个过滤器 20 System.out.println("Within SimpleFilter1:Filtering the Response..."); 21 } catch (IOException ioe) { 22 ioe.printStackTrace(); 23 } catch (ServletException se) { 24 se.printStackTrace(); 25 } 26 } 27 public void destroy() { 28 this.filterConfig = null; 29 } 30 }
1 package test_servlet_package; 2 3 import java.io.IOException; 4 import javax.servlet.Filter; 5 import javax.servlet.FilterChain; 6 import javax.servlet.FilterConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.ServletRequest; 9 import javax.servlet.ServletResponse; 10 11 public class SimpleFilter2 implements Filter { 12 @SuppressWarnings("unused") 13 private FilterConfig filterConfig; 14 15 public void init(FilterConfig config) throws ServletException { 16 this.filterConfig = config; 17 } 18 19 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { 20 try { 21 System.out.println("Within SimpleFilter2:Filtering the Request..."); 22 chain.doFilter(request, response); // 把处理发送到下一个过滤器 23 System.out.println("Within SimpleFilter2:Filtering the Response..."); 24 } catch (IOException ioe) { 25 ioe.printStackTrace(); 26 } catch (ServletException se) { 27 se.printStackTrace(); 28 } 29 } 30 31 public void destroy() { 32 this.filterConfig = null; 33 } 34 /* 35 * Within SimpleFilter1:Filtering the Request... Within 36 * SimpleFilter2:Filtering the Request... Within SimpleFilter2:Filtering the 37 * Response... Within SimpleFilter1:Filtering the Response... 38 */ 39 }
1 package test_servlet_package; 2 import java.io.IOException; 3 import javax.servlet.ServletConfig; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 public class test_servlet extends HttpServlet { 10 private static final long serialVersionUID = 1L; 11 // servlet 第一次 请求 被初始化 ,对象 在 容器 里 只有 一个 12 // 但是 在 web.xml 配置 了 <load-on-startup>1</load-on-startup> 则 会 立刻 启动初始化 13 // 初始化 14 public void init(ServletConfig config) throws ServletException { 15 super.init(config); 16 // 从配置文件中获得初始化参数 17 String test = config.getInitParameter("test"); 18 ServletContext context = this.getServletContext(); 19 String filePath = context.getRealPath(test); 20 System.out.println("filePath:" + filePath); 21 } 22 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 23 ServletConfig config = this.getServletConfig(); 24 ServletContext servletContext = this.getServletContext(); 25 res.getWriter().println("test"); 26 res.getWriter().close(); 27 28 } 29 protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 30 doGet(req, res); 31 } 32 }