过滤器的使用
开发人员从创建一个或多个实现javax.servlet.Filter 接口的java类开始,这些类可以再servlet请求处理之前采取一些动作,换句话说,它先于与之相关的servlet或jsp页面运行于服务器上。在请求发送到其目标之前创建一系列动作(包括完全阻塞请求)。把不要的东西挡住,要的东西留向目标。
在Servlet作为过滤器使用时,它可以对客户的请求进行处理、处理完成后,他会交给下一个过滤器处理这样,客户的请求在过滤器链里逐个处理,直到请求发送到目标为止。
过滤器API中包含3个简单的接口 FIlter、FilterChain、FilterConfig。
过滤器类实现Filter 接口,然后使用这个过滤器类中的FilterChain 和FilterConfig 接口。该过滤器类的一个引用将传递给FilterChain对象,以允许过滤器把控制权传递给链中的下一个资源。FilterConfig 对象将容器提供给过滤器,以允许访问该过滤器的初始化数据。
在web.xml中使用filter和filter-mapping元素关联过滤器和servlet。
运用过滤器设置编码格式:
Servlet:
1 import java.io.IOException; 2 import java.io.PrintWriter; 3 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class MyServlet extends HttpServlet { 10 11 public void doGet(HttpServletRequest request, HttpServletResponse response) 12 throws ServletException, IOException { 13 doPost(request, response); 14 } 15 16 public void doPost(HttpServletRequest request, HttpServletResponse response) 17 throws ServletException, IOException { 18 PrintWriter out=response.getWriter(); 19 out.print("hello Filter"); 20 } 21 22 }
Filter:
1 import java.io.IOException; 2 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 10 public class MyFilter implements Filter { 11 private static String CODE="utf-8"; 12 @Override 13 public void destroy() { 14 15 } 16 17 @Override 18 public void doFilter(ServletRequest arg0, ServletResponse arg1, 19 FilterChain arg2) throws IOException, ServletException { 20 System.out.println(" Do Filter.."); 21 arg0.setCharacterEncoding(CODE); 22 arg1.setCharacterEncoding(CODE); 23 arg2.doFilter(arg0, arg1); 24 } 25 26 @Override 27 public void init(FilterConfig arg0) throws ServletException { 28 29 } 30 31 }
XML:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>Test</display-name> 4 <servlet> 5 <servlet-name>MyServlet</servlet-name> 6 <servlet-class>test.MyServlet</servlet-class> 7 </servlet> 8 <servlet-mapping> 9 <servlet-name>MyServlet</servlet-name> 10 <url-pattern>/MyServlet</url-pattern> 11 </servlet-mapping> 12 <filter> 13 <filter-name>MyFilter</filter-name> 14 <filter-class>test.MyFilter</filter-class> 15 </filter> 16 <filter-mapping> 17 <filter-name>MyFilter</filter-name> 18 <servlet-name>/MyServlet</servlet-name> 19 <url-pattern>/MyFilter</url-pattern> 20 </filter-mapping> 21 <welcome-file-list> 22 <welcome-file>index.jsp</welcome-file> 23 </welcome-file-list> 24 </web-app>
在部署文件中使用flter-mapping元素将每个过滤器映射到Servlet,这些过滤器将按照特闷在部署文件中出现的顺序应用于该Servlet。
为过滤器配置初始化参数:
使用filter元素的init-param子元素来声明初始化参数及其值。在过滤器中通过调用FilterConfig对象的getInitParameter方法访问参数。
改进编码过滤器:
1 import java.io.IOException; 2 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 10 public class MyFilter implements Filter { 11 12 public MyFilter() { 13 // TODO Auto-generated constructor stub 14 } 15 16 @Override 17 public void destroy() { 18 // TODO Auto-generated method stub 19 20 } 21 22 @Override 23 public void doFilter(ServletRequest arg0, ServletResponse arg1, 24 FilterChain arg2) throws IOException, ServletException { 25 System.out.println(" Do Filter.."); 26 arg2.doFilter(arg0, arg1); 27 } 28 29 @Override 30 public void init(FilterConfig arg0) throws ServletException { 31 // TODO Auto-generated method stub 32 String CODE = arg0.getInitParameter("CODE"); 33 } 34 35 }
1 <filter> 2 <filter-name>MyFilter</filter-name> 3 <filter-class>test.MyFilter</filter-class> 4 <init-param> 5 <param-name>CODE</param-name> 6 <param-value>UTF-8</param-value> 7 </init-param> 8 </filter>