- ServletContext的事件监听器,创建:当前web应用被加载(或重新加载)到服务器中,销毁:当前web应用被卸载
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* ServletContext的事件监听器,创建:当前web应用被加载(或重新加载)到服务器中,销毁:当前web应用被卸载
*/
public class ApplicationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("当前web应用被加载到服务器中,创建ServletContextListener对象...");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("当前web应用被卸载,销毁ServletContextListener对象...");
}
}
- HttpSession对象的监听器,创建:会话的开始,销毁:session调用invalidate()方法,或者会话时间到期
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* HttpSession对象的监听器,创建:会话的开始,销毁:session调用invalidate()方法,或者会话时间到期
*/
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("监听HttpSession对象的创建...");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("监听HttpSession对象的销毁...");
}
}
- HttpSession对象的监听器,创建:一次请求的开始,销毁:一次请求的结束
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
/**
* HttpSession对象的监听器,创建:一次请求的开始,销毁:一次请求的结束
*
*/
public class RequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("监听ServletRequest对象的销毁...");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("监听ServletRequest对象的创建...");
}
}
页面:
demo1.jsp
<body>
<h3>Demo1 Page</h3>
<!-- 1.请求页面 -->
<!-- 使用该方法无法跨页面传递参数 -->
<a href="demo2.jsp">To test Page</a>
<%
//为request对象设置一个属性
request.setAttribute("name", "test1");
%>
<!-- 使用forward方法可以跨页面传递参数 -->
<jsp:forward page="demo2.jsp"></jsp:forward>
</body>
demo2.jsp
<body>
<h3>销毁session</h3>
<%
session.invalidate();
%>
name:<%=request.getAttribute("name") %>
<br><br>
<!-- 2.请求Servlet -->
<a href="testServlet">TestServlet </a>
</body>
demo3.jsp
<body>
<h3>Demo3 Page</h3>
name:<%=request.getAttribute("name") %>
</body>
session.jsp
<body>
<h3>Session Page</h3>
<%
//在page标签中禁用掉session,然后显式的创建一个session
HttpSession session = request.getSession();
//销毁session,1.设置过期时间,2.调用invalidate方法
session.setMaxInactiveInterval(10);
//session.invalidate();
%>
</body>
web.xml配置文件
<!-- 1.监听域对象自身的创建和销毁的事件监听器 -->
<listener>
<listener-class>com.test.demo1.ServletContextListener.ApplicationListener</listener-class>
</listener>
<listener>
<listener-class>com.test.demo1.HttpSessionListener.SessionListener</listener-class>
</listener>
<listener>
<listener-class>com.test.demo1.ServletRequestListener.RequestListener</listener-class>
</listener>
<!-- 2.监听域对象中属性的增加和删除的事件监听器 -->
<listener>
<listener-class>com.test.demo2.HttpSessionAttributeListener.SessionAttributeListener</listener-class>
</listener>
<listener>
<listener-class>com.test.demo2.ServletContextAttributeListener.ApplicationAttributeListener</listener-class>
</listener>
<listener>
<listener-class>com.test.demo2.ServletRequestAttributeListener.RequestAttributeListener</listener-class>
</listener>
结果:
信息: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [232] milliseconds.
当前web应用被加载到服务器中,创建ServletContextListener对象...
Application中添加属性:org.apache.jasper.compiler.TldLocationsCache :org.apache.jasper.compiler.TldLocationsCache@1bdb367
十一月 30, 2017 2:54:53 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-apr-8084"]
十一月 30, 2017 2:54:53 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-apr-8009"]
十一月 30, 2017 2:54:53 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 1386 ms
监听ServletRequest对象的创建...
Request中替代属性:org.apache.catalina.ASYNC_SUPPORTED :true
Application中添加属性:org.apache.jasper.runtime.JspApplicationContextImpl :org.apache.jasper.runtime.JspApplicationContextImpl@52ccef60
监听HttpSession对象的创建...
Request中添加属性:name :test1
监听HttpSession对象的销毁...
监听ServletRequest对象的销毁...
监听ServletRequest对象的创建...
Request中替代属性:org.apache.catalina.ASYNC_SUPPORTED :true
Request中添加属性:name :test2
监听ServletRequest对象的销毁...
监听ServletRequest对象的创建...
Request中替代属性:org.apache.catalina.ASYNC_SUPPORTED :true
监听HttpSession对象的创建...
监听ServletRequest对象的销毁...