zoukankan      html  css  js  c++  java
  • struts2 javaweb 过滤器、监听器 拦截器 原理

    转:

    过滤器、监听器 拦截器

    过滤器 创建一个 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>

  • 相关阅读:
    地图相关
    爬虫机器人检测网站
    Git 工作区、暂存区和版本库概念
    linux镜像下载地址
    selenium基本使用
    socket 编程
    视频观看时间统计
    油猴脚本
    (II)第十三节:使用注解创建Dao、Service、Controller Bean 组件
    (II)第十一节:SpEL 表达式
  • 原文地址:https://www.cnblogs.com/nucdy/p/5761111.html
Copyright © 2011-2022 走看看