1. java web中监听器的阐述
Listener是Servlet的监听器,它可以监听客户端的请求,服务端的操作等,通过监听器,可以自动激发一些操作,比如监听在线的用户的数量,当然这只是近似,因为session存于内存中,浏览器关闭后,服务器中的session并没有被销毁
Spring框架就用到了ServletContextListener监听器,使应用在启动的时候就去构建一个springMVC的子容器。
Spring还用到了DispatcherServlet这个类,它的间接父类当然也是HttpServlet
所以不管是Struts框架、Spring框架,底层都是基于Servlet/Jsp技术的,基础的重要性不言而喻
2. ServletContextListener使用
该接口主要含有两个方法ContextDestroyed(ServletContextEvent sce)以及ContextInitialized(ServletContextEvent sce)
如果在web.xml中配置了实现ServletContextListener接口的listener,那么web应用启动的过程中,首先会执行该实现类的ContextInitialized方法,然后如果配置了Filter那么再去执行Filter的init方法
在搭建springMVC的过程中,我们在web.xml中会有如下的配置:
<listener>
<listenerclass>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
该类其实就是ServletContextListener的实现类而已
public class ContextLoaderListener extends ContextLoader
implements ServletContextListener {
public ContextLoaderListener() {
}
public ContextLoaderListener(WebApplicationContext context) {
super(context);
}
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
/**
* Close the root web application context.
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
}
springMVC框架选择在这个时机来初始化springMVC子容器是非常明智的,因为ServletContextListener在web应用启动过程中就会被执行,这个时候还没有任何请求过来,便于spring读取相应的配置文件来构建mvc子容器
当然在容器进行重启的时候,首先会卸载掉该容器中的Servlet对象,然后再卸载掉Filter对象,最后卸载掉实现ServletContextListener接口的实例,有点类似于栈的形式,初始化的时候先是ServletContextListener实现类,接着是Filter实现类,最后是Servlet实现类,而卸载的时候则逆之
3. ServletContextAttributeListener使用
首先需要在web.xml中配置listener元素,配置完后,当服务端有向application或ServletContext中添加、修改、删除内容的时候,会触发该监听器的相应事件
web.xml中配置如下:
<listener>
<listener-class>
com.shengsiyuan.listener.MyServletContextAttributeListener
</listener-class>
</listener>
ServletContextAttributeListener实现类中的代码如下:
public class MyServletContextAttributeListener implements
ServletContextAttributeListener {
public void attributeAdded(ServletContextAttributeEvent arg0) {
System.out.println("MyServletContextAttributeListener added "
+ arg0.getName() + ":" + arg0.getValue());
}
public void attributeRemoved(ServletContextAttributeEvent arg0) {
System.out.println("MyServletContextAttributeListener removed "
+ arg0.getName() + ":" + arg0.getValue());
}
public void attributeReplaced(ServletContextAttributeEvent arg0) {
System.out.println("MyServletContextAttributeListener replaced "
+ arg0.getName() + ":" + arg0.getValue());
}
}
JSP中的代价如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<% application.setAttribute("aa", "bb"); %>
<% application.setAttribute("aa", "cc"); %>
<% application.removeAttribute("aa"); %>
</body>
</html>
请求该JSP时控制台输出结果如下:
MyServletContextAttributeListener added aa:bb
MyServletContextAttributeListener replaced aa:bb
MyServletContextAttributeListener removed aa:cc
4. HttpSessionListener使用
该类提供了两个方法:sessionCreated(HttpSessionEvent arg0)、sessionDestroyed(HttpSessionEvent arg0),在web.xml中配置好该监听器后,那么当session被创建及销毁的时候会自动调用上述的相应方法,使用方式类似于ServletContextListener,在这里就不过多介绍了
5. HttpSessionAttributeListener使用
提供了如下几个方法:
public void attributeAdded(HttpSessionBindingEvent arg0) {
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
}
使用方式类似于ServletContextAttributeListener,在这不过多介绍了
6. 总结
java web中的监听器还是必须知道的,这有利于之后的框架学习,否则学习之后的框架时,一头雾水,并且需要掌握监听器的本源观察者模式
7. 文章引用来源
<<张龙JavaEE系列视频>>
网友评论