拦截器,过滤器,监听器原理

过滤器

创建一个Filter 只需两个步骤: 
(1)创建Filter 处理类: 

(2)在web.xml 文件中配置Filter 。 
创建Filter 必须实现javax.servlet.Filter 接口,在该接口中定义了三个方法。 
• void init(FilterConfig config): 用于完成Filter 的初始化。 
• void destroy(): 用于Filter 销毁前,完成某些资源的回收。 
• void doFilter(ServletRequest request, ServletResponse response,FilterChain chain): 实现过滤功能,该方法就是对每个请求及响应增加的额外处理。 

过滤器Filter也具有生命周期:init()->doFilter()->destroy(),由部署文件中的filter元素驱动。在servlet2.4中,过滤器同样可以用于请求分派器,但须在web.xml中声明,<dispatcher>INCLUDE或FORWARD或REQUEST或ERROR</dispatcher>该元素位于filter-mapping中。 

一、理解Struts2拦截器

1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.

2. 拦截器栈(Interceptor Stack)。Struts2拦截器栈就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,Struts2拦截器链中的拦截器就会按其之前定义的顺序被调用。

二、实现Struts2拦截器原理

Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的    拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器

三、定义Struts2拦截器。

Struts2规定用户自定义拦截器必须实现com.opensymphony.xwork2.interceptor.Interceptor接口。该接口声明了3个方法,

void init();

void destroy();

String intercept(ActionInvocation   invocation) throws Exception;

其中,init和destroy方法会在程序开始和结束时各执行一遍,不管使用了该拦截器与否,只要在struts.xml中声明了该Struts2拦截器就会被执行。
intercept方法就是拦截的主体了,每次拦截器生效时都会执行其中的逻辑。

不过,struts中又提供了几个抽象类来简化这一步骤。

public abstract class AbstractInterceptor   implements Interceptor;

public abstract class   MethodFilterInterceptor extends AbstractInterceptor;

都是模板方法实现的。

其中AbstractInterceptor提供了init()和destroy()的空实现,使用时只需要覆盖intercept()方法;

而MethodFilterInterceptor则提供了includeMethods和excludeMethods两个属性,用来过滤执行该过滤器的action的方法。可以通过param来加入或者排除需要过滤的方法。

一般来说,拦截器的写法都差不多。看下面的示例:

package interceptor;

import   com.opensymphony.xwork2.ActionInvocation;

import   com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements   Interceptor {

public void destroy() {

// TODO Auto-generated method stub

}

public void init() {

// TODO Auto-generated method stub

}

public String intercept(ActionInvocation   invocation) throws Exception {

System.out.println("Action执行前插入 代码");       

//执行目标方法 (调用下一个拦截器, 或执行Action)     

final String res =   invocation.invoke();   

System.out.println("Action执行后插入 代码");   

return res;   

}

}

四、配置Struts2拦截器

Struts2拦截器需要在struts.xml中声明,如下struts.xml配置文件

<?xml version="1.0"   encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD   Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant   name="struts.objectFactory" value="spring" />

 

<package name="default" extends="struts-default">

<interceptors>

<interceptor   name="MyInterceptor"   class="interceptor.MyInterceptor"></interceptor>

<interceptor-stack   name="myInterceptorStack">

<interceptor-ref   name="MyInterceptor"/>

<interceptor-ref   name="defaultStack"/>

</interceptor-stack>

</interceptors>

<action name="loginAction"   class="loginAction">

<result   name="fail">/index.jsp </result>

<result   name="success">/success.jsp</result>

<interceptor-ref   name="myInterceptorStack"></interceptor-ref>

</action>

</package>

</struts>

 

 

 

拦截器与过滤器的区别

  1. 拦截器是基于java的反射机制的,而过滤器是基于函数回调。
  2. 拦截器不依赖与servlet容器,过滤器依赖与servlet容器。
  3. 拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用。
  4. 拦截器可以访问action上下文、值栈里的对象,而过滤器不能访问。
  5. 在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次

 

 

    监听器概述   

    1.Listener是Servlet的监听器    

  2.可以监听客户端的请求、服务端的操作等。   

  3.通过监听器,可以自动激发一些操作,如监听在线用户数量,当增加一个HttpSession时,给在线人数加1。   

  4.编写监听器需要实现相应的接口   

  5.编写完成后在web.xml文件中配置一下,就可以起作用了   

  6.可以在不修改现有系统基础上,增加web应用程序生命周期事件的跟踪   

  

  3.HttpSessionListener   常用接口

  监听HttpSession的操作。当创建一个Session时,激发session Created(SessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。   

  4.HttpSessionAttributeListener   

  监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。   

  

使用范例:   

由监听器管理共享数据库连接   

  

  生命周期事件的一个实际应用由context监听器管理共享数据库连接。在web.xml中如下定义监听器:   

<listener>   

    <listener-class>XXX.MyConnectionManager</listener-class>   

</listener> ?server创建监听器的实例,接受事件并自动判断实现监听器接口的类型。要记住的是由于监听器是配置在部署描述符web.xml中,所以不需要改变任何代码就可以添加新的监听器。   

  

public class MyConnectionManager implements ServletContextListener{     

  public void contextInitialized(ServletContextEvent e) {    

        Connection con = // create connection    

        e.getServletContext().setAttribute("con", con);    

    }     

   public void contextDestroyed(ServletContextEvent e) {    

        Connection con = (Connection) e.getServletContext().getAttribute("con");    

        try {   

          con.close();    

        }    

       catch (SQLException ignored) { } // close connection    

    }    

}     

  监听器保证每新生成一个servlet context都会有一个可用的数据库连接,并且所有的连接对会在context关闭的时候随之关闭。    

  

  

计算在线用户数量的Linstener   

(1)   

Package xxx;    

  

public class OnlineCounter {     

   private static long online = 0;        

   public static long getOnline(){   

      return online;   

    }   

    public static void raise(){    

       online++;   

    }   

    public static void reduce(){    

       online--;    

   }   

}    

  

import javax.servlet.http.HttpSessionEvent;   

import javax.servlet.http.HttpSessionListener;   

  

public class OnlineCounterListener implements HttpSessionListener{   

    public void sessionCreated(HttpSessionEvent hse) {    

        OnlineCounter.raise();     

    }    

   public void sessionDestroyed(HttpSessionEvent hse){     

        OnlineCounter.reduce();   

    }    

}    

  

在需要显示在线人数的JSP中可是使用   

目前在线人数:   

<%@ page import=“xxx.OnlineCounter" %>  

<%=OnlineCounter.getOnline()%>  

 

退出会话(可以给用户提供一个注销按钮):  

<form action="exit.jsp" method=post>   

  <input type=submit value="exit">    

</form>    

  

exit.jsp: <%session.invalidate() ;%>    

  

在web.xml中加入:   

<listener>    

  <listener-class>servletlistener111111.SecondListener</listener-class> </listener>

 

posted @ 2013-04-19 22:30  江南香  阅读(422)  评论(0编辑  收藏  举报