过滤器、监听器、拦截器

来源:互联网 发布:海岛奇兵升船数据 编辑:程序博客网 时间:2024/05/17 02:44

过滤器:

使用Servlet中的过滤器Filter,首先类需要实现javax.servlet.Filter接口,其工作原理是,只要你在web.xml文件配置好要拦截的客户端请求,它都会帮你拦截到请求,此时你就可以对请求或响应(Request、Response)统一设置编码,简化操作;同时还可进行逻辑判断,如用户是否已经登陆、有没有权限访问该页面等等工作。它是随你的web应用启动而启动的,只初始化一次,以后就可以拦截相关请求,只有当你的web应用停止或重新部署的时候才销毁。

例如:

类FilterTest.java

public class FilterTest implements Filter{

  @Override

  public void destroy() {

    }

  @Override

  public void doFilter(ServletRequest arg0, ServletResponse arg1,

      FilterChain chain) throws IOException,ServletException {

                 System.out.println("每次请求都会首先执行这里的方法!");

     //将请求转发给过滤器链上下一个对象,如果还有过滤器,则是下一个过滤器,否则就是访问的对象

      chain.doFilter(request,response);

  }

  @Override

  public void init(FilterConfig arg0) throws ServletException {

    }

}

web.xml中的配置:

<filter>  

      <filter-name>filter</filter-name>  

      <filter-class>filtertest.FilterTest</filter-class>  

  </filter>  

  <filter-mapping>  

      <filter-name>filter</filter-name>  

      <url-pattern>/*</url-pattern>  

  </filter-mapping>

这样,所有的请求,都会首先执行过滤器内的内容,过滤器拦截的顺序是web.xml中配置的顺序。


监听器:

Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。

用于监听 ServletContext 对象的创建和删除以及属性的添加、删除和修改等操作,该监听器需要用到如下两个接口类:

(1)ServletContextAttributeListener:监听对 ServletContext 属性的操作,比如增加、删除、修改

     attributeAdded(ServletContextAttributeEvent e)             添加属性时调用   

     attributeReplaced(ServletContextAttributeEvent e)        修改属性时调用   

      attributeRemoved(ServletContextAttributeEvente)        删除属性时调用

 

(2)ServletContextListener:监听对 ServletContext  对象的创建和删除

      contextInitialized(ServletContextEventsce)          初始化时调用   

      contextDestroyed(ServletContextEventsce)        销毁时调用,即当服务器重新加载时调用


用于监听 HTTP 会话活动情况和 HTTP 会话中的属性设置情况,也可以监听 HTTP 会话的 active 和 passivate 情况等,该监听器需要用到如下多个接口类:

(1) HttpSessionListener:监听 HttpSession 的操作

      sessionCreate(HttpSessionEvent se)         初始化时调用;

      sessionDestroyed(httpSessionEventse)    销毁时调用,即当用户注销时调用

(2)HttpSessionActivationListener:用于监听 HTTP 会话的 active 和 passivate 情况

(3)HttpSessionAttributeListener:监听 HttpSession 中的属性操作

      attributeAdded(HttpSessionBindingEventse)         添加属性时调用

      attributeRemoved(HttpSessionBindingEventse)    删除属性时调用

      attributeReplaced(HttpSessionBindingEventse)    修改属性时调用

例如,监听session的创建和销毁:

首先创建类ListenerTest.java,实现HttpSessionListener接口:

public class ListenerTest implementsHttpSessionListener{

  @Override

  public void sessionCreated(HttpSessionEventevent) {

    System.out.println("create");

  }

  @Override

  public void sessionDestroyed(HttpSessionEventevent) {

    System.out.println("destroy");

  }}

web.xml配置:

<listener> 

  <listener-class>listenerTest.ListenerTest</listener-class> 

</listener>

当创建session时(创建后首次获取),会触发sessionCreated,销毁session时request.getSession().invalidate(),触发sessionDestroyed


用于对客户端的请求进行监听是在 Servlet2.4 规范中新添加的一项新技术,使用的接口如下:

(1)ServletRequestListener 接口类

       requestDestroyed(ServletRequestEvente)       对销毁客户端进行监听,即当执行request.removeAttribute("xxx") 时调用

       requestInitialized(ServletRequestEvente)         对实现客户端的请求进行监听

(2)ServletRequestAttributeListener 接口类

      attributeAdded(ServletRequestAttributeEvent e)         对属性添加进行监听

       attributeRemoved(ServletRequestAttributeEvente)    对属性删除进行监听

      attributeReplaced(ServletRequestAttributeEvent e)    对属性替换进行监听


拦截器:

面向切面技术等使用反射、动态代理的方式。(待续)

原创粉丝点击