`
zhaolicric
  • 浏览: 143808 次
  • 性别: Icon_minigender_2
  • 来自: 青岛
社区版块
存档分类
最新评论

web.xml中--使用监听器Servlet

阅读更多

监听器概述   
  
1.Listener是Servlet的监听器    
2.可以监听客户端的请求、服务端的操作等。   
3.通过监听器,可以自动激发一些操作,如监听在线用户数量,当增加一个HttpSession时,给在线人数加1。   
4.编写监听器需要实现相应的接口   
5.编写完成后在web.xml文件中配置一下,就可以起作用了   
6.可以在不修改现有系统基础上,增加web应用程序生命周期事件的跟踪   
  
  
常用的监听接口   
  
1.ServletContextAttributeListener   
监听对ServletContext属性的操作,比如增加/删除/修改   
2.ServletContextListener   
监听ServletContext,当创建ServletContext时,激发 contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。   
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关闭的时候随之关闭。     
  
在web.xml中加入:   
<listener><listener-class>servletlistener111111.SecondListener</listener-class> </listener>

==================================================

关于用户超时的例子:

public class OnlineUserListener implements HttpSessionListener {
    public void sessionCreated(HttpSessionEvent event) {
    }
    public void sessionDestroyed(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        ServletContext application = session.getServletContext();
        // 取得登录的用户名
        String username = (String) session.getAttribute("username");
        // 从在线列表中删除用户名
        List onlineUserList = (List) application.getAttribute("onlineUserList");
        onlineUserList.remove(username);
        System.out.println(username + "超时退出。");
    }
}
 

 

以下两种情况下就会发生sessionDestoryed(会话销毁)事件:

1.执行session.invalidate()方法时。例如:request.getSession().invalidate();

2.如果用户长时间没有访问服务器,超过了会话最大超时时间,服务器就会自动销毁超时的session。会话超时时间可以在web.xml中进行设置。

========================================

使用HttpSessionBindingListener

HttpSessionBindingListener虽然叫做监听器,但使用方法与HttpSessionListener完全不同。我们实际看一下它是如何使用的。

我们的OnlineUserBindingListener实现了HttpSessionBindingListener接口,接口中共定义了两个方法:valueBound()和valueUnbound(),分别对应数据绑定,和取消绑定两个事件。

所谓对session进行数据绑定,就是调用session.setAttribute()把HttpSessionBindingListener保存进session中。我们在LoginServlet.java中进行这一步。

// 把用户名放入在线列表
session.setAttribute("onlineUserBindingListener", new OnlineUserBindingListener(username));
       
这就是HttpSessionBindingListener和HttpSessionListener之间的最大区 别:HttpSessionListener只需要设置到web.xml中就可以监听整个应用中的所有session。 HttpSessionBindingListener必须实例化后放入某一个session中,才可以进行监听。

从监听范围上比较,HttpSessionListener设置一次就可以监听所有session,HttpSessionBindingListener通常都是一对一的。

正是这种区别成就了HttpSessionBindingListener的优势,我们可以让每个listener对应一个username,这样 就不需要每次再去session中读取username,进一步可以将所有操作在线列表的代码都移入listener,更容易维护。

valueBound()方法的代码如下:

public void valueBound(HttpSessionBindingEvent event) {
    HttpSession session = event.getSession();
    ServletContext application = session.getServletContext();

    // 把用户名放入在线列表
    List onlineUserList = (List) application.getAttribute("onlineUserList");
    // 第一次使用前,需要初始化
    if (onlineUserList == null) {
        onlineUserList = new ArrayList();
        application.setAttribute("onlineUserList", onlineUserList);
    }
    onlineUserList.add(this.username);
}
       
username已经通过构造方法传递给listener,在数据绑定时,可以直接把它放入用户列表。

与之对应的valueUnbound()方法,代码如下:

public void valueUnbound(HttpSessionBindingEvent event) {
    HttpSession session = event.getSession();
    ServletContext application = session.getServletContext();

    // 从在线列表中删除用户名
    List onlineUserList = (List) application.getAttribute("onlineUserList");
    onlineUserList.remove(this.username);

    System.out.println(this.username + "退出。");
}
       
这里可以直接使用listener的username操作在线列表,不必再去担心session中是否存在username。

valueUnbound的触发条件是以下三种情况:

1.执行session.invalidate()时。

2.session超时,自动销毁时。

3.执行session.setAttribute("onlineUserListener", "其他对象");或session.removeAttribute("onlineUserListener");将listener从session中删除时。

因此,只要不将listener从session中删除,就可以监听到session的销毁。

分享到:
评论

相关推荐

    JSPservlet中web.xml详细配置指南(包含所有情况)

    JSP/Servlet 中 web.xml 详细配置指南 web.xml 是 Java Web 应用程序的核心配置文件,它定义了 Web 应用的结构和行为。在 JSP/Servlet 中,web.xml 扮演着关键角色,用于配置 Web 应用程序的各个方面。本文将对 web...

    Tomcat中用web.xml控制Web应用详解

    Tomcat 中 web.xml 文件是 Web 应用的核心配置文件,负责管理 Web 应用的生命周期、Servlet 的加载顺序、Filter 的配置等。下面对 web.xml 文件中的重要元素进行详细解释。 context-param 元素 context-param 元素...

    J2EE中关于web.xml文件的配置

    通过 web.xml 文件,我们可以对 Web 应用进行配置,例如设置应用程序的名称、描述、过滤器、监听器、Servlet、会话超时等等。 以下是 web.xml 文件中的一些常用元素: 1. `&lt;web-app&gt;`:web.xml 文件的根元素,用于...

    JSP Web.xml标准配置内容

    --在该例中在IE地址栏中firstservlet字符串对应到别名为firstservlet的servlet--&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;firstservlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/firstservlet&lt;/url-pattern&gt; &lt;/servlet-mapping&gt;...

    tomcat配置文件web.xml与server.xml解析

    在 Web 应用程序中,事件监听器和过滤器可以在 web.xml 文件中进行配置,例如: &lt;listener-class&gt;com.example.MyServletContextListener&lt;/listener-class&gt; 在上面的配置中,定义了一个名为 ...

    用web.xml控制Web应用的行为

    用web.xml控制Web应用的行为 目录 1 定义头和根元素 2 2 部署描述符文件内的元素次序 2 3 分配名称和定制的UL 3 3.1 分配名称 3 3.2 定义定制的URL 5 3.3 命名JSP页面 6 4 禁止激活器...

    web.xml标签说明.docx

    `&lt;listener-class&gt;` 元素指定监听器类的完整的限定类名。 9. `&lt;servlet&gt;` 元素:用于声明一个 Servlet。 `&lt;servlet-name&gt;` 指定 servlet 的名字,这个名字在同一个 Web 应用程序中必须是唯一的。 `&lt;servlet-class&gt;...

    web.xml配置详解

    部署描述符实际上是一个XML文件,包含了很多描述servlet/JSP应用的各个方面的元素,如servlet注册、servlet映射以及监听器注册

    springweb3.0MVC注解(附实例)

    web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: &lt;?xml...

    webservice服务中WSServletContextListener监听器所需要的jar包

    解决异常:Error configuring application listener of class com.sun.xml.ws.transport.http.servlet.WSServletContextListener java.lang.ClassNotFoundException: ...在web项目中发布webservice服务所依赖的jar包

    CXF发布WebService的多种方法实例

    2、把web.xml里的spring的监听器注释掉,保证WEB-INF下有cxf-servlet.xml,然后发布到WEB服务器即可 3、web.xml里配置spring监听器,及其加载的beans.xml,把cxf-servlet.xml删掉,发布到WEB服务器即可,注意这种方式...

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    一、Web项目中使用Spring ...在 Web 项目中使用 Spring 框架,首先要解决在 web 层(这里指 Servlet)中获取到 Spring容器的问题。只要在 web 层获取到了 Spring 容器,便可从容器中获取到 Service 对象

    新版JSP+JQUERY+AJAX+Mysql聊天室小程序JASChatWeb1.0

    JASChatWeb 网页聊天室 1.0版--群聊+... ServetLogout.java -- 两个监听器,一个负责监听用户session超时,另一个用于服务器关闭时清除用户登陆状态 -----------------------------------------------------------------

    Java™ Servlet 规范.

    1.6.1 监听器(Listener)顺序 ...............................................................................................................14 1.6.2 注解处理 .............................................

    关于JSP配置文件web.xml加载顺序详解

    4、容器创建中的类实例,创建监听器。 二、 load-on-startup 元素在web应用启动的时候指定了servlet被加载的顺序,它的值必须是一个整数。如果它的值是一个负整数或是这个元素不存在,那么容器会在该s

    我收集的servlet中事件监听器机制we吧xml配置详解

    我收集的servlet中事件、监听器机制、web.xml配置详解

    servlet监听器案例

    servlet监听器案例,web.xml,加jsp,listener的配置和使用

    JSP与Servlet 技术总结

    JSP & Servlet 技术总结 JSP技术总结 2 1. JSP页面元素构成 2 2. JSP脚本元素 2 3. 注释 2 4. JSP指令 2 5. JSP动作元素 3 6. JSP内置对象 5 7. JSP内置对象及其作用 5 ...10. 监听器如何使用? 13

    servlet监听器

    在web.xml中配置监听器 servlet

Global site tag (gtag.js) - Google Analytics