1. HttpSessionActivationListener
1). 监听实现了该接口和 Serializable 接口的 Java 类的对象随 session 钝化和活化事件
> 活化: 从磁盘中读取 session 对象
> 钝化: 向磁盘中写入 session 对象
> session 对象存储在tomcat 服务器的 workCatalinalocalhostcontextPath 目录下. SESSION.SER
2). 该监听器不需要在 web.xml 文件中进行配置.
3).
/ /在活化之后被调用.
public void sessionDidActivate(HttpSessionEvent se)
/ /在钝化之前被调用
public void sessionWillPassivate(HttpSessionEvent se)
HttpSessionEvent: getSession()
4). 该监听器较少被使用.
2. HttpSessionBindingListener
1). 监听实现了该接口的 Java 类的对象被绑定到 session 或从 session 中解除绑定的事件.
//当前对象被绑定到 session 时调用该方法
public void valueBound(HttpSessionBindingEvent event)
//当前对象从 session 中解除绑定调用该方法
public void valueUnbound(HttpSessionBindingEvent event)
2). 注意: 该监听器不需要在 web.xml 文件中进行配置.
3). HttpSessionBindingEvent:
getName()
getValue()
getSession()
4). 该监听器较少被使用.
3. XxxAttributeListener
1). 监听 ServletContext, HttpSession, ServletRequest 中添加属性, 替换属性, 移除属性的事件监听器.
2). 以 ServletRequestAttributeListener 为例:
//添加属性时被调用
public void attributeAdded(ServletRequestAttributeEvent srae) {
System.out.println("向 request 中添加了一个属性...");
}
//移除属性时被调用
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
System.out.println("从 request 中移除了一个属性...");
}
//替换属性时被调用.
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
System.out.println("request 中属性替换了...");
}
3). 这三个 ServletContextAttributeListener,
ServletRequestAttributeListener, HttpSessionAttributeListener 监听器较少被使用.
4). API:
ServletRequestAttributeEvent:
> getName(): 获取属性的名字
> getValue(): 获取属性的值.
4. ServletRequestListener & HttpSessionListener
1). 和 ServletContextListener 类似。
2). 利用 ServletRequestListener、HttpSessionListener 以及 ServletContextListener 可以把 request, session
及 application 的生命周期进一步的做一了解.
> request: 是一个请求, 当一个响应返回时, 即被销毁. 当发送一个请求时被创建. 注意, 请求转发的过程是一个 request 对象.
重定向是两个请求
> session: 当第一次访问 WEB 应用的一个 JSP 或 Servlet 时, 且该 JSP 或 Servlet 中还需要创建 session 对象. 此时服务器会
创建一个 session 对象.
session 销毁: session 过期, 直接调用 session 的 invalidate 方法, 当前 web 应用被卸载(session 可以被持久化).
* 关闭浏览器, 并不意味着 session 被销毁, 还可以通过 sessionid 找到服务器中的 session 对象.
JSESSIONID=F4119DE0FC93ED38E8EC83B24CFA3B81
http://localhost:8989/day_40/session.jsp;jsessionid=F4119DE0FC93ED38E8EC83B24CFA3B81
> application: 贯穿于当前的 WEB 应用的生命周期. 当前 WEB 应用被加载时创建 application 对象, 当前 WEB 应用被卸载时
销毁 application 对象.
5. ServletContextListener:
1). what: 监听 ServletContext 对象被创建或销毁的 Servlet 监听器
2). how:
> 创建一个实现了 ServletContextListener 的类, 并且实现其中的两个方法
public class HelloServletContextListner implements ServletContextListener
> 在 web.xml 文件中配置 Listener
<listener>
<listener-class>com.atguigu.javaweb.test.HelloServletContextListner</listener-class>
</listener>
3). why: ServletContextListener 是最常用的 Listener, 可以在当前 WEB 应用被加载时对当前 WEB 应用的相关资源进行初始化操作:
创建数据库连接池, 创建 Spring 的 IOC 容器, 读取当前 WEB 应用的初始化参数 ...
4). API:
/ / SerlvetContext 对象被创建(即, 当前 WEB 应用被加载时)的时候, Servlet 容器调用该方法.
public void contextInitialized(ServletContextEvent sce)
// SerlvetContext 对象被销毁之前(即, 当前 WEB 应用被卸载时)的时候, Servlet 容器调用该方法.
public void contextDestroyed(ServletContextEvent sce)
ServletContextEvent 中的: getServletContext() 获取 ServletContext
6.代码区
package com.atguigu.javaweb.test; import java.io.Serializable; import javax.servlet.http.HttpSessionActivationListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; import javax.servlet.http.HttpSessionEvent; public class Customer implements Serializable,HttpSessionBindingListener, HttpSessionActivationListener{ private String time; public void setTime(String time) { this.time = time; } /** * */ private static final long serialVersionUID = 1L; @Override public void valueBound(HttpSessionBindingEvent event) { System.out.println("绑定到 session"); Object value = event.getValue(); System.out.println(value == this); System.out.println(event.getName()); } @Override public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("从 sessoin 中解除绑定"); } @Override public void sessionWillPassivate(HttpSessionEvent se) { System.out.println("从内存中写到磁盘上..."); } @Override public void sessionDidActivate(HttpSessionEvent se) { System.out.println("从磁盘中读取出来..."); } @Override public String toString() { return super.toString() + ", time: " + time; } }
package com.atguigu.javaweb.test; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class HelloServletContextListner implements ServletContextListener, ServletRequestListener, HttpSessionListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContext 对象被创建。 " + sce.getServletContext()); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContext 对象被销毁。" + sce.getServletContext()); } @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("HttpSession 被创建。"); } @Override public void sessionDestroyed(HttpSessionEvent se) { // TODO Auto-generated method stub System.out.println("HttpSession 被销毁"); } @Override public void requestDestroyed(ServletRequestEvent sre) { // TODO Auto-generated method stub System.out.println("ServletRequest 被销毁"); } @Override public void requestInitialized(ServletRequestEvent sre) { // TODO Auto-generated method stub System.out.println("ServletRequest 被创建"); } }
package com.atguigu.javaweb.test; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class TestAttributeListener implements ServletContextAttributeListener, ServletRequestAttributeListener, HttpSessionAttributeListener{ @Override public void attributeAdded(ServletRequestAttributeEvent srae) { System.out.println("向 request 中添加了一个属性: " + srae.getName() + ": " + srae.getValue()); } @Override public void attributeRemoved(ServletRequestAttributeEvent srae) { System.out.println("从 request 中移除了一个属性: " + srae.getName() + ": " + srae.getValue()); } @Override public void attributeReplaced(ServletRequestAttributeEvent srae) { System.out.println("request 中属性替换了: " + srae.getName() + ": " + srae.getValue()); } @Override public void attributeAdded(ServletContextAttributeEvent scab) { // TODO Auto-generated method stub } @Override public void attributeRemoved(ServletContextAttributeEvent scab) { // TODO Auto-generated method stub } @Override public void attributeReplaced(ServletContextAttributeEvent scab) { // TODO Auto-generated method stub } @Override public void attributeAdded(HttpSessionBindingEvent se) { // TODO Auto-generated method stub } @Override public void attributeRemoved(HttpSessionBindingEvent se) { // TODO Auto-generated method stub } @Override public void attributeReplaced(HttpSessionBindingEvent se) { // TODO Auto-generated method stub } } /*ServletRequest 被创建 request 中属性替换了: org.apache.catalina.ASYNC_SUPPORTED: true 向 request 中添加了一个属性: name: ABCD ----------------------------- request 中属性替换了: name: ABCD ----------------------------- 从 request 中移除了一个属性: name: DEFG ----------------------------- ServletRequest 被销毁 */
package com.atguigu.javaweb.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestServlet */ public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("requestKey", "requestValue2"); request.getRequestDispatcher("/test.jsp").forward(request, response); // response.sendRedirect("test.jsp"); } }
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <listener> <listener-class>com.atguigu.javaweb.test.TestAttributeListener</listener-class> </listener> <listener> <listener-class>com.atguigu.javaweb.test.HelloServletContextListner</listener-class> </listener> <servlet> <description></description> <display-name>TestServlet</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>com.atguigu.javaweb.test.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> </web-app>
<%@page import="java.util.Date"%> <%@page import="com.atguigu.javaweb.test.Customer"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h4>HttpSessionBindingListener</h4> <%-- Customer customer = new Customer(); session.setAttribute("customer", customer); System.out.println("------------------------"); session.removeAttribute("customer"); --%> <% Customer customer = (Customer)session.getAttribute("customer"); if(customer == null){ customer = new Customer(); customer.setTime("" + new Date()); session.setAttribute("customer", customer); System.out.println("创建一个新的 Customer 对象: " + customer + ", 并放入到 session 中"); }else{ System.out.println("从 session 中读取到 Customer 对象: " + customer); } %> <!-- ServletRequest 被创建 request 中属性替换了: org.apache.catalina.ASYNC_SUPPORTED: true 绑定到 session true customer 创建一个新的 Customer 对象: com.atguigu.javaweb.test.Customer@2f089b0d, time: Wed Jan 17 15:03:43 CST 2018, 并放入到 session 中 ServletRequest 被销毁 --> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h4>Index Page</h4> <a href="test.jsp">To Test Page</a> <%-- request.setAttribute("requestKey", "requestValue"); <jsp:forward page="/test.jsp"></jsp:forward> --%> <br><br> <a href="TestServlet">TestServlet</a> <!-- ServletRequest 被创建 request 中属性替换了: org.apache.catalina.ASYNC_SUPPORTED: true HttpSession 被创建。 ServletRequest 被销毁 --> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h4>Session Page</h4> <% HttpSession session = request.getSession(true); //session.setMaxInactiveInterval(10); session.invalidate(); %> <!-- ServletRequest 被创建 request 中属性替换了: org.apache.catalina.ASYNC_SUPPORTED: true HttpSession 被创建。 HttpSession 被销毁 ServletRequest 被销毁 --> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% request.setAttribute("name", "ABCD"); System.out.println("-----------------------------"); request.setAttribute("name", "DEFG"); System.out.println("-----------------------------"); request.removeAttribute("name"); System.out.println("-----------------------------"); %> <!-- ServletRequest 被创建 request 中属性替换了: org.apache.catalina.ASYNC_SUPPORTED: true HttpSession 被创建。 向 request 中添加了一个属性: name: ABCD ----------------------------- request 中属性替换了: name: ABCD ----------------------------- 从 request 中移除了一个属性: name: DEFG ----------------------------- ServletRequest 被销毁 --> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>Test Page</h4>
<%= request.getAttribute("requestKey") %>
</body>
</html>