Loading

servlet中的8个Listener

servlet中的8个Listener:

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>TestServlet4</servlet-name>
        <servlet-class>com.stono.servlet.TestServlet4</servlet-class>
        <init-param>
            <param-name>testServlet4ParamName</param-name>
            <param-value>testServlet4ParamValue</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet4</servlet-name>
        <url-pattern>/test4.do</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextParamName</param-name>
        <param-value>contextParamValue</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <listener>
        <listener-class>com.stono.servlet.listeners.MyServletContextListener</listener-class>
        <listener-class>com.stono.servlet.listeners.MyServletContextAttributeListener</listener-class>
        <listener-class>com.stono.servlet.listeners.MyHttpSessionListener</listener-class>
        <listener-class>com.stono.servlet.listeners.MyHttpSessionAttributeListener</listener-class>
        <listener-class>com.stono.servlet.listeners.MyServletRequestListener</listener-class>
        <listener-class>com.stono.servlet.listeners.MyServletRequestAttributeListener</listener-class>
        <!-- 不知道这个Listener是否需要在这里面配置 -->
        <listener-class>com.stono.servlet.listeners.MyHttpSessionActivationListener</listener-class>
    </listener>
</web-app>

 

Listener:

package com.stono.servlet.listeners;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
/* 文档中提到进行JVM迁移的时候才能激发此事件; */
public class MyHttpSessionActivationListener implements HttpSessionActivationListener {
    @Override
    public void sessionDidActivate(HttpSessionEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyHttpSessionActivationListener.sessionDidActivate(HttpSessionEvent)");
    }
    @Override
    public void sessionWillPassivate(HttpSessionEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyHttpSessionActivationListener.sessionWillPassivate(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);
    }
}
package com.stono.servlet.listeners;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class MyHttpSessionBindingListener implements HttpSessionBindingListener {
    private String name;
    private String value;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    @Override
    /* 将此对象设置到session中的时候触发的事件; */
    public void valueBound(HttpSessionBindingEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyHttpSessionBindingListener.valueBound(HttpSessionBindingEvent)");
        String name2 = arg0.getName();
        Object value2 = arg0.getValue();
        Object source = arg0.getSource();
        System.out.println("name:" + name2 + ";value:" + value2 + ";source:" + source);
    }
    @Override
    /* session.invalide()的时候触发的事件; */
    public void valueUnbound(HttpSessionBindingEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyHttpSessionBindingListener.valueUnbound(HttpSessionBindingEvent)");
        String name2 = arg0.getName();
        Object value2 = arg0.getValue();
        Object source = arg0.getSource();
        System.out.println("name:" + name2 + ";value:" + value2 + ";source:" + source);
    }
}
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.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
    @Override
    /* 进行context.setAttribute的时候调用 */
    public void attributeAdded(ServletContextAttributeEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyServletContextAttributeListener.attributeAdded(ServletContextAttributeEvent)");
        String name = arg0.getName();
        Object value = arg0.getValue();
        System.out.println("name=" + name + ";value=" + value);
    }
    @Override
    /* 进行context.removeAttribute的时候调用 */
    public void attributeRemoved(ServletContextAttributeEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyServletContextAttributeListener.attributeRemoved(ServletContextAttributeEvent)");
        String name = arg0.getName();
        Object value = arg0.getValue();
        System.out.println("name=" + name + ";value=" + value);
    }
    @Override
    /* 不知何时调用? */
    public void attributeReplaced(ServletContextAttributeEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyServletContextAttributeListener.attributeReplaced(ServletContextAttributeEvent)");
    }
}
package com.stono.servlet.listeners;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener {
    @Override
    /* tomcat自动重新Reload Context的时候会调用; */
    public void contextDestroyed(ServletContextEvent arg0) {
        String x = "com.stono.servlet.listeners.MyServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent)";
        System.out.println(x);
    }
    @Override
    /* Context初始化的时候会调用; */
    public void contextInitialized(ServletContextEvent arg0) {
        String x = "com.stono.servlet.listeners.MyServletContextListener.contextInitialized(javax.servlet.ServletContextEvent)";
        System.out.println(x);
    }
}
package com.stono.servlet.listeners;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {
    @Override
    /* 在request.setAttribute()方法调用的时候触发; */
    public void attributeAdded(ServletRequestAttributeEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyServletRequestAttributeListener.attributeAdded(ServletRequestAttributeEvent)");
        String name = arg0.getName();
        Object value = arg0.getValue();
        System.out.println("name:" + name + ";value:" + value);
    }
    @Override
    /* 在request.removeAttribute()的时候触发; */
    public void attributeRemoved(ServletRequestAttributeEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyServletRequestAttributeListener.attributeRemoved(ServletRequestAttributeEvent)");
        String name = arg0.getName();
        Object value = arg0.getValue();
        System.out.println("name:" + name + ";value:" + value);
    }
    @Override
    /* 在第二次调用request.setAttribute()的时候触发此方法; */
    public void attributeReplaced(ServletRequestAttributeEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyServletRequestAttributeListener.attributeReplaced(ServletRequestAttributeEvent)");
        String name = arg0.getName();
        Object value = arg0.getValue();
        System.out.println("name:" + name + ";value:" + value);
    }
}
package com.stono.servlet.listeners;
import java.util.Enumeration;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class MyServletRequestListener implements ServletRequestListener {
    @Override
    /* 请求结束之后调用这个事件 */
    public void requestDestroyed(ServletRequestEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyServletRequestListener.requestDestroyed(ServletRequestEvent)");
    }
    @Override
    /* 请求到来的时候调用这个事件 */
    public void requestInitialized(ServletRequestEvent arg0) {
        System.out.println("com.stono.servlet.listeners.MyServletRequestListener.requestInitialized(ServletRequestEvent)");
        Enumeration<String> names = arg0.getServletRequest().getParameterNames();
        while(names.hasMoreElements()){
            System.out.println(names.nextElement());
        }
    }
}

 

Servlet:

package com.stono.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.stono.servlet.listeners.MyHttpSessionActivationListener;
import com.stono.servlet.listeners.MyHttpSessionBindingListener;
public class TestServlet4 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = req.getServletContext();
        Enumeration<String> names = getServletConfig().getInitParameterNames();
        Enumeration<String> names2 = context.getInitParameterNames();
        System.out.println("javax.servlet.ServletConfig.getInitParameterNames()");
        while (names.hasMoreElements()) {
            System.out.println(names.nextElement());
        }
        System.out.println("javax.servlet.ServletContext.getInitParameterNames()");
        while (names2.hasMoreElements()) {
            System.out.println(names2.nextElement());
        }
        context.setAttribute("contextSetAttributeFromSevlet4Name", "contextSetAttributeFromSevlet4Value");
        context.setAttribute("contextParamName", "contextSetAttributeFromSevlet4Value2");
        context.removeAttribute("contextParamName");
        HttpSession session = req.getSession();
        // session.getAttributeNames();
        session.setAttribute("sessionAttrInTestServlet4", "sessionAttrInTestServlet4");
        try {
            // Servlet中可以使用这样的方式进行sleep,一个Servlet只有一个实例,多个线程;
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        session.setAttribute("sessionAttrInTestServlet4", "sessionAttrInTestServlet4_2");
        session.invalidate();
        try {
            Thread.sleep(4000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("just before request.setAttribute");
        req.setAttribute("a", "a");
        System.out.println("just after request.setAttribute");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        req.setAttribute("a", "a2");
        System.out.println("just after request.setAttribute second time");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        req.removeAttribute("a");
        System.out.println("just after request.removeAttribute");
        session = req.getSession();
        MyHttpSessionBindingListener obj = new MyHttpSessionBindingListener();
        session.setAttribute("obj", obj);
        obj.setName("namename");
        obj.setValue("valuevalue");
        System.out.println("set session object second time");
        session.setAttribute("obj", obj);
        session.invalidate();
        System.out.println("MyHttpSessionActivationListener testing...");
        MyHttpSessionActivationListener activationListener = new MyHttpSessionActivationListener();
        session = req.getSession();
        session.setAttribute("activation", activationListener);
        PrintWriter writer = resp.getWriter();
        writer.println("this is from TestServlet4");
    }
}

 

posted @ 2015-09-29 15:45  stono  阅读(350)  评论(0编辑  收藏  举报