首先来看一下Servlet的过滤器内容:
一、Servlet过滤器的概念:
******************************************************************************************************************************************************************************
十二、一个实例
首先来看一下web.xml的配置:
<!-- 请求url日志记录过滤器 --> <filter> <filter-name>logfilter</filter-name> <filter-class>com.weijia.filterservlet.LogFilter</filter-class> </filter> <filter-mapping> <filter-name>logfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 编码过滤器 --> <filter> <filter-name>setCharacterEncoding</filter-name> <filter-class>com.weijia.filterservlet.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>setCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
package com.weijia.filterservlet; import java.io.IOException; import java.util.Enumeration; import java.util.HashMap; 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 EncodingFilter implements Filter { private String encoding; private HashMap<String,String> params = new HashMap<String,String>(); // 项目结束时就已经进行销毁 public void destroy() { System.out.println("end do the encoding filter!"); params=null; encoding=null; } public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException { System.out.println("before encoding " + encoding + " filter!"); req.setCharacterEncoding(encoding); chain.doFilter(req, resp); System.out.println("after encoding " + encoding + " filter!"); System.err.println("----------------------------------------"); } // 项目启动时就已经进行读取 public void init(FilterConfig config) throws ServletException { System.out.println("begin do the encoding filter!"); encoding = config.getInitParameter("encoding"); for (Enumeration<?> e = config.getInitParameterNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); String value = config.getInitParameter(name); params.put(name, value); } } }
package com.weijia.filterservlet; 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.http.HttpServletRequest; public class LogFilter implements Filter { public FilterConfig config; public void destroy() { this.config = null; System.out.println("end do the logging filter!"); } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { System.out.println("before the log filter!"); // 将请求转换成HttpServletRequest 请求 HttpServletRequest hreq = (HttpServletRequest) req; // 记录日志 System.out.println("Log Filter已经截获到用户的请求的地址:"+hreq.getServletPath() ); try { // Filter 只是链式处理,请求依然转发到目的地址。 chain.doFilter(req, res); } catch (Exception e) { e.printStackTrace(); } System.out.println("after the log filter!"); } public void init(FilterConfig config) throws ServletException { System.out.println("begin do the log filter!"); this.config = config; } }
package com.weijia.filterservlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FilterServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setDateHeader("expires", -1); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
运行结果:
before the log filter!
Log Filter已经截获到用户的请求的地址:/FilterServlet
before encoding utf-8 filter!
after encoding utf-8 filter!
----------------------------------------
after the log filter!
我们从运行结果可以看到这个过滤器的调用关系:
类似于C++中的构造函数和析构函数的调用顺序,
这里我们在web.xml中注册的是先注册日志过滤器的,然后再注册
当我们重新部署应用的时候发现:
会先销毁上次的过滤器,然后再重新注册一下
下面在来看一下Servlet的监听器
Servlet监听器用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。下面将介绍几种常用的监听器,以及它们都适合运用于那些环境。
分类及介绍:
1. ServletContextListener:用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口。
2. ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。
3. HttpSessionListener:用于监听Session对象的创建和销毁,监听器类需要实现javax.servlet.http.HttpSessionListener接口或者javax.servlet.http.HttpSessionActivationListener接口,或者两个都实现。
4. HttpSessionActivationListener:用于监听Session对象的钝化/活化事件,监听器类需要实现javax.servlet.http.HttpSessionListener接口或者javax.servlet.http.HttpSessionActivationListener接口,或者两个都实现。
5. HttpSessionAttributeListener:用于监听Session对象属性的改变事件,监听器类需要实现javax.servlet.http.HttpSessionAttributeListener接口。
部署:
监听器的部署在web.xml文件中配置,在配置文件中,它的位置应该在过滤器的后面Servlet的前面
web.xml配置文件: