Java web应用之监听器

来源:互联网 发布:西安那家网络宽带好 编辑:程序博客网 时间:2024/05/01 17:47

最近使用到了监听器,用来在服务器启动时加载配置文件,启动与数据库的连接,简单的使用了ServletContextListener,此时再来总结一下,顺带把监听器的方方面面都梳理一下,已达到由点及面的学习效果。
监听器在项目中实例:

  • 首先抛出我们在项目中使用监听器的案例。我们在项目中使用的是用于监听应用程序环境对象的事件监听器,也就是ServletContextListener,它随着服务器启动而开始监听,随服务器关闭而停止监听。代码:
public class Scheduler implements ServletContextListener{    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);    @Override    public void contextInitialized(ServletContextEvent arg0) {              WebApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());    }    /*     * 服务器关闭时,将会进行的操作     */    @Override    public void contextDestroyed(ServletContextEvent arg0) {        DataCollector.getDataCollector().clear();        executor.shutdown();    }}

实现监听器就是创建类Scheduler,实现接口ServletContextListener,此时会有两个方法需要实现:

public void contextInitialized(ServletContextEvent arg0)public void contextDestroyed(ServletContextEvent arg0)

一个是在服务器启动的时候执行,另一个是在服务器关闭的时候执行。接下来在web.xml中进行配置:

<listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <listener>    <listener-class>com.mon.cache.Scheduler</listener-class>  </listener>

服务器会按照文件中书写顺序加载listener,我们的Scheduler放在第二个,第一个是spring加载配置文件,生成一大堆bean的,这个当然要放在前面。我们在上述的代码中也用到了spring加载后的好处:

WebApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());

在web.xml中配以:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/BJFM_applicationContext*.xml</param-value>  </context-param>

这样在服务器一启动就获得了applicationContext。当然了,此处还涉及到一个自动周期调用线程的方式,以后再说。

  • 监听器分类:

按照监听的对象划分:

  1. 用于监听应用程序环境对象(ServletContext)的事件监听器;
  2. 用于监听用户会话对象(HttpSession)的事件监听器;
  3. 用于监听请求消息对象(ServletRequest)的事件监听器;

按照监听的事件划分:

  1. 监听域对象自身的创建和销毁的事件监听器;
  2. 监听域对象中的属性的增加和删除的事件监听器;
  3. 监听绑定到HttpSession域中的某个对象的状态的事件监听器;

  • 监听域对象自身的创建和销毁的事件监听器

    域对象有三种:ServletContext、HttpSession、ServletRequest。为监听这三种域对象,Servlet中专门定义了三种对应的接口:
    ServletContextListener、HttpSessionListener、ServletRequestListener。

ServletContextListener:

一个项目中有多个监听ServletContext对象的ServletContextListener,但是只有一个ServletContext对象。
ServletContextListener中方法contextInitialized(ServletContextEvent arg0)中的参数ServletContextEvent代表了ServletContext这个对象。用如下办法就可以获取在web.xml中配置的:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/classes/BJFM_applicationContext*.xml</param-value>  </context-param>

HttpSessionListener:

一个项目中有多个监听HttpSession对象的HttpSessionListener,但是只有一个HttpSession对象。
同样有两个方法,在session创建和销毁时被调用:

public void sessionCreated(HttpSessionEvent arg0)public void sessionDestroyed(HttpSessionEvent arg0)

session被创建的时机:打开浏览器登陆服务器的时候;session被销毁的时机:关闭服务器、关闭浏览器、未关闭浏览器,但session超时。可以在web.xml中配置超时时间。

ServletRequestListener:

一个项目中有多个监听ServletRequest对象的ServletRequestListener,但是只有一个ServletRequest对象。
同样有两个方法,在request创建和销毁时被调用:

public void requestInitialized(ServletRequestEvent arg0)public void requestDestroyed(ServletRequestEvent arg0)

可以用以下手段获取到request中带着的参数:

arg0.getServletRequest().getParameter("name");

  • 监听域对象中的属性的增加和删除的事件监听器
ServletContextAttributeListenerHttpSessionAttributeListenerServletRequestAttributeListener

  • 监听绑定到HttpSession域中的某个对象的状态的事件监听器

httpSession中的对象状态:绑定(保存)——解除绑定、钝化(session对象持久化到一个存储设备上)——活化(将session对象从一个存储对象上进行恢复)。
钝化机制
正常的session都保存在服务器内存中,当有大量用户在线时,对内存消耗太大,就需要把不经常使用的session序列化到系统文件或数据库中。使用时再反序列化到内存。
Tomcat中session钝化机制由sessionManager管理:

1.StandardManger

当Tomcat被关闭或重启时,服务器会将当前内存中的session对象钝化到服务器文件系统中;
Web应用程序被重新加载时,内存中的Session对象也会被钝化到服务器的文件系统中。
钝化后的文件被保存在SESSIONS.ser中

2.Persistentmanager

可以配置主流内存的Session对象数目,将不常使用的Session对象保存到文件系统或数据库,当用时再重新加载。
钝化驱动类:FileStore、JDBCStore
HttpSessionBindingListener
绑定
解除绑定
不需要再web.xml中注册
HttpSessionActivationListener
钝化
活化
不需要再web.xml中注册
需要实现序列化的接口


  • 监听器在servlet3.0下的用法:

使用@Weblistener可以将类声明为监听器,当然,被标注的类需要实现上面的接口,但是无法定义监听器启动顺序。


总结

上面说了三种监听器,实际项目中运用比较多的是“监听域对象自身的创建和销毁的事件监听器”,在统计网站在线人数时,利用如下语句可以将用户列表放入ServletContext中,因为ServletContext伴随服务器整个运行时期,所以set到ServletContext的Attribute中就可以随时读取使用。
ServletRequestEvent.getServletContext().setAttribute(“userList”, userList);

在jsp页面中可以使用如下语句获取userList:

<%    ArrayList<com.mon.entity.User> userList = (ArrayList<com.mon.entity.User>)request.getServletContext().getAttribute("userList");     if(userList != null){        for(int i=0; i < userList.size(); i++){            com.mon.entity.User user = userList.get(i);        }    }%>
0 0
原创粉丝点击