一、 Context、Request、Session作用域与生命周期
在介绍JavaWeb:Listener监听器ServletContextListener、ServletRequestListener、HttpSessionListener之前,需要对 Context、Request、Session 对象的作用域和生命周期 有基础的了解。这里先回顾下三者的生命周期。
类别 | init 初始化 | destroy销毁 | 作用域 |
---|---|---|---|
Context | web项目服务启动后 | web项目关闭 | 整个application项目 |
Request | 每个请求进来时 | 每个请求结束时 | 只在单个Request对象线程中 |
Session | 当Request请求创建Session时 | 当Session失效过期时 | 只在具体的Session对象范围内。 |
初步简单的回顾下 Context、Request、Session 对象的作用域和生命周期 。
接下来是介绍Listener监听器。
二、JavaWeb内的Listener们
主要介绍以下6个Listener监听器:
ServletContextListener、ServletContextAttributeListener
ServletRequestListener、ServletRequestAttributeListener
HttpSessionListener、HttpSessionAttributeListener
这里按两个维度理解这几个Listener监听器。
① 生命周期维度
ServletContextListener、ServletRequestListener、HttpSessionListener 这三个都是按照各自的生命周期中init初始化和destroy销毁时触发对应的方法。
如下图:
ServletContextListner ServletRequestListener HttpSessionListener
这三者的接口中都是init初始化/create创建 方法 和 destroyed销毁方法,都是在对应生命周期的对应时刻触发。
② 字段变更维度
ServletContextAttributeListener、ServletRequestAttributeListener、HttpSessionAttributeListener 这三个是安装各自在字段变更时触发对应的 Added()、Removed()、Replaced() 方法。
ServletContextAttributeListener ServletRequestAttributeListener HttpSessionAttributeListener这三者的接口中都是在字段属性发生变更时触发对应的 Added()、Removed()、Replaced() 方法。
三、测试代码
① MyContextListener 类
public class MyContextListener implements ServletContextListener, ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scae) {
System.out.println("context [added] name: " + scae.getName() + " = " + scae.getValue());
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {
System.out.println("context [removed] name: " + scae.getName() + " = " + scae.getValue());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae) {
System.out.println("context [replaced] name: " + scae.getName() + " = " + scae.getValue());
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("context [init] name: " + sce.getServletContext());
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("context [destroyed] name: " + sce.getServletContext());
}
}
② MyRequestListener类
public class MyRequestListener implements ServletRequestListener,ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
System.out.println("request [added] name: " + srae.getName() + " = " + srae.getValue());
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
System.out.println("request [removed] name: " + srae.getName() + " = " + srae.getValue());
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
System.out.println("request [replaced] name: " + srae.getName() + " = " + srae.getValue());
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("request [destroyed] name: " + sre.getServletRequest());
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("request [init] name: " + sre.getServletRequest());
}
}
③ MySessionListener类
public class OnlineCountListener implements HttpSessionListener, HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("http session attribute add : " + se.getName() + " = " + se.getValue());
}
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("http session attribute removed : " + se.getName() + " = " + se.getValue());
}
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("http session attribute replaced : " + se.getName() + " = " + se.getValue());
}
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("http session [created] :" + se.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("http session [destroyed] :" + se.getSession().getId());
}
}
④ 用于访问的OnlineServlet类
public class OnlineServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// context测试
getServletContext().setAttribute("context", "context");
getServletContext().setAttribute("context", "context");
getServletContext().removeAttribute("context");
// reqeust测试
req.setAttribute("req", "req");
req.setAttribute("req", "req");
req.removeAttribute("req");
// session测试
HttpSession session = req.getSession();
session.setAttribute("a", "a");
session.setAttribute("a", "b");
session.removeAttribute("a");
session.setMaxInactiveInterval(3);
resp.getWriter().write(" sessionId: " + session.getId());
}
}
⑤ web.xml配置
<listener>
<listener-class>com.jx.listener.MyRequestListener</listener-class>
</listener>
<listener>
<listener-class>com.jx.listener.MyContextListener</listener-class>
</listener>
<listener>
<listener-class>com.jx.listener.MySessionListener</listener-class>
</listener>
<servlet>
<servlet-name>online</servlet-name>
<servlet-class>com.jx.listener.OnlineServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>online</servlet-name>
<url-pattern>/online</url-pattern>
</servlet-mapping>
① 运行结果
启动项目-> 访问Servlet -> 关闭项目
context [init] name: org.apache.catalina.core.ApplicationContextFacade@149c182
context [add] name: org.apache.jasper.compiler.TldLocationsCache = org.apache.jasper.compiler.TldLocationsCache@12012fb
request [init] name: org.apache.catalina.connector.RequestFacade@1ed2fb0
request [replaced] name: org.apache.catalina.ASYNC_SUPPORTED = true
context [add] name: context = context
context [replaced] name: context = context
context [removed] name: context = context
request [add] name: req = req
request [replaced] name: req = req
request [removed] name: req = req
http session [created] :27270467C78BF21720DF220EE4CF7428
http session attribute [add] : sessionid : = 27270467C78BF21720DF220EE4CF7428
http session attribute [add] : a = a
http session attribute [replaced] : a = a
http session attribute [removed] : a = b
request [destroyed] name: org.apache.catalina.connector.RequestFacade@1ed2fb0
http session [destroyed] :27270467C78BF21720DF220EE4CF7428
http session attribute [removed] : sessionid : = 27270467C78BF21720DF220EE4CF7428
context [destroyed] name: org.apache.catalina.core.ApplicationContextFacade@149c182
分析下步骤:
- context 先初始化 [init]
- context 启动后会自动往attribute里塞东西,所以触发了 [add]
- 访问Servlet ,所以触发了 request [init]
- request 创建后系统触发了 request [replaced] --系统的
- 代码里触发了 context [add] ,代码:
getServletContext().setAttribute("context", "context");
- 代码里触发了 context [replaced] ,代码:
getServletContext().setAttribute("context", "context");
- 代码里触发了 context [removed],代码:
getServletContext().removeAttribute("context");
- 代码里触发了 request [add], 代码:
req.setAttribute("req", "req");
- 代码里触发了 request [replaced], 代码:
req.setAttribute("req", "req");
- 代码里触发了 request [removed], 代码:
req.removeAttribute("req");
- 代码里触发了 http session [created], 代码:
HttpSession session = req.getSession();
- 代码里触发了 http session attribute [add] , 代码:
session.setAttribute("sessionid : ", session.getId());
- 代码里触发了 http session attribute [add] , 代码:
session.setAttribute("a", "a");
- 代码里触发了 http session attribute [replaced], 代码:
session.setAttribute("a", "b");
- 代码里触发了 http session attribute [removed], 代码:
session.removeAttribute("a");
- request访问结束,所以触发了request [destroyed]
- 设置了session过期时间,所以触发了http session [destroyed],代码:
session.setMaxInactiveInterval(3);
- session的destroy方法里触发了http session attribute [removed]方法。
- 关闭了项目,触发了 context [destroyed]
结束~
网友评论