Web应用中监听者的通知顺序按照DD中的定义顺序

Web应用中监听者的通知顺序按照DD中的定义顺序:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>TestBootWeb</display-name>
<servlet>
<servlet-name>TestServlet1</servlet-name>
<servlet-class>com.stono.servlet.listenerorder.TestServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet1</servlet-name>
<url-pattern>/a.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.stono.servlet.listeners.MyHttpSessionListener2</listener-class>
<listener-class>com.stono.servlet.listeners.MyHttpSessionAttributeListener</listener-class>
<listener-class>com.stono.servlet.listeners.MyHttpSessionListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Listener:

package com.stono.servlet.listeners;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MyHttpSessionListener implements HttpSessionListener {
@Override
/* 必须调用一下session.getAttributeNames();才可以激活这个事件; */
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("com.stono.servlet.listeners.MyHttpSessionListener.sessionCreated(HttpSessionEvent)");
}
@Override
/* 调用session.invalidate()的时候激活这个事件; */
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("com.stono.servlet.listeners.MyHttpSessionListener.sessionDestroyed(HttpSessionEvent)");
}
}
package com.stono.servlet.listeners;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MyHttpSessionListener2 implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("com.stono.servlet.listeners.MyHttpSessionListener2.sessionCreated(HttpSessionEvent)");
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("com.stono.servlet.listeners.MyHttpSessionListener2.sessionDestroyed(HttpSessionEvent)");
}
}
package com.stono.servlet.listeners;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {
@Override
/* 调用session.setAttribute的时候触发事件; */
public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("com.stono.servlet.listeners.MyHttpSessionAttributeListener.attributeAdded(HttpSessionBindingEvent)");
String name = arg0.getName();
Object value = arg0.getValue();
System.out.println("name:" + name + ";value:" + value);
}
@Override
/* 调用session.invalidate()方法之后触发这个事件; */
public void attributeRemoved(HttpSessionBindingEvent arg0) {
System.out.println("com.stono.servlet.listeners.MyHttpSessionAttributeListener.attributeRemoved(HttpSessionBindingEvent)");
String name = arg0.getName();
Object value = arg0.getValue();
System.out.println("name:" + name + ";value:" + value);
}
@Override
/* 第二次调用session.setAttribute的时候会触发此事件,但是arg0.getValue()还是返回之前的值 */
public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("com.stono.servlet.listeners.MyHttpSessionAttributeListener.attributeReplaced(HttpSessionBindingEvent)");
String name = arg0.getName();
Object value = arg0.getValue();
System.out.println("name:" + name + ";value:" + value);
}
}

Servlet:

package com.stono.servlet.listenerorder;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class TestServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setAttribute("a", "a");
PrintWriter writer = resp.getWriter();
writer.println("this is from com.stono.servlet.listenerorder.TestServlet1");
}
}

输出:

com.stono.servlet.listeners.MyHttpSessionListener2.sessionCreated(HttpSessionEvent)
com.stono.servlet.listeners.MyHttpSessionListener.sessionCreated(HttpSessionEvent)
com.stono.servlet.listeners.MyHttpSessionAttributeListener.attributeAdded(HttpSessionBindingEvent)
name:a;value:a
com.stono.servlet.listeners.MyHttpSessionListener.sessionDestroyed(HttpSessionEvent)
com.stono.servlet.listeners.MyHttpSessionListener2.sessionDestroyed(HttpSessionEvent)
com.stono.servlet.listeners.MyHttpSessionAttributeListener.attributeRemoved(HttpSessionBindingEvent)
name:a;value:a
上一篇:猜数字和飞机大战(Python零基础入门)


下一篇:centos(linux) 下如何查看端口占用情况及杀死进程