标签:
继续接着监听器的分类进行,来看第二种分类中的第二小类。
MyServletContextAttributeListener.java源代码:
package com.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listener implementation class MyServletContextAttributeListener
*
*/
@WebListener
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
/**
* Default constructor.
*/
public MyServletContextAttributeListener() {
}
/**
* @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
*/
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("ServletContext_attributeAdded:"+servletContextAttributeEvent.getName());
}
/**
* @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
*/
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("ServletContext_attributeRemoved:"+servletContextAttributeEvent.getName());
}
/**
* @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
*/
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("ServletContext_attributeReplaced:"+servletContextAttributeEvent.getName());
}
}
MyHttpSessionAttributeListener.java源代码:
package com.listener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
/**
* Application Lifecycle Listener implementation class MyHttpSessionAttributeListener
*
*/
@WebListener
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {
/**
* Default constructor.
*/
public MyHttpSessionAttributeListener() {
}
/**
* @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
*/
public void attributeAdded(HttpSessionBindingEvent sessionBindingEvent) {
System.out.println("HttpSession_attributeAdded:"+sessionBindingEvent.getName());
}
/**
* @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
*/
public void attributeRemoved(HttpSessionBindingEvent sessionBindingEvent) {
System.out.println("HttpSession_attributeRemoved:"+sessionBindingEvent.getName());
}
/**
* @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
*/
public void attributeReplaced(HttpSessionBindingEvent sessionBindingEvent) {
System.out.println("HttpSession_attributeReplaced:"+sessionBindingEvent.getName());
}
}
ServletRequestAttributeListener.java源代码:
package com.listener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listener implementation class MyServletRequestAttributeListener
*
*/
@WebListener
public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {
/**
* Default constructor.
*/
public MyServletRequestAttributeListener() {
}
/**
* @see ServletRequestAttributeListener#attributeRemoved(ServletRequestAttributeEvent)
*/
public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {
System.out.println("ServletRequest_attributeRemoved:"+servletRequestAttributeEvent.getName());
}
/**
* @see ServletRequestAttributeListener#attributeAdded(ServletRequestAttributeEvent)
*/
public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {
System.out.println("ServletRequest_attributeAdded:"+servletRequestAttributeEvent.getName());
}
/**
* @see ServletRequestAttributeListener#attributeReplaced(ServletRequestAttributeEvent)
*/
public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {
System.out.println("ServletRequest_attributeReplaced:"+servletRequestAttributeEvent.getName());
}
}
我们使用的JSP页面包括:
index.jsp页面:
<%@ 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>
<h1>我是JSP页面</h1>
<hr/>
<button onclick="location.href=‘<%=request.getContextPath()%>/init.jsp‘;">Init</button>
<button onclick="location.href=‘<%=request.getContextPath()%>/destory.jsp‘;">Destory</button>
</body>
</html>
init.jsp页面:
<%@page import="com.domain.Admin"%>
<%@ 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>
<h1>这是初始化值的页面</h1>
<hr/>
<%
request.setAttribute("requestName", "requestValue");
request.getSession().setAttribute("sessionName", "sessionValue");
request.getSession().getServletContext().setAttribute("contextName", "contextValue");
%>
<button onclick="location.href=‘<%=request.getContextPath()%>/init.jsp‘;">Init</button>
<button onclick="location.href=‘<%=request.getContextPath()%>/destory.jsp‘;">Destory</button>
</body>
</html>
destory.jsp页面:
<%@ 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>
<h1>这是销毁值的页面</h1>
<hr/>
<%
request.removeAttribute("requestName");
request.getSession().removeAttribute("sessionName");
request.getSession().getServletContext().removeAttribute("contextName");
%>
<button onclick="location.href=‘<%=request.getContextPath()%>/init.jsp‘;">Init</button>
<button onclick="location.href=‘<%=request.getContextPath()%>/destory.jsp‘;">Destory</button>
</body>
</html>
运行结果:
我们通过访问index页面开始:
点击init按钮:
再次点击init按钮:
点击destory按钮:
我们来看第三小类。
本质就在于把服务器中不经常使用的Session对象暂时序列化到系统文件系统或者数据库系统中,当使用时反序
列化到内存中,整个过程由服务器自动完成。使用的并不是很多。
示意图:
Tomcat中两种session钝化管理器
session钝化机制由SessionManager管理。
第一种:
第二种:
Servlet规范
包括两个监听器,都不需要再web.xml中注册。
绑定:vlaueBound()方法
解除绑定:valueUnbound()方法
钝化:sessionWillPassivate()方法
活化:sessionDidActivate()方法
只提供上述两个监听器的实例源码:
package com.domain;
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 User implements HttpSessionBindingListener,HttpSessionActivationListener,Serializable {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void valueBound(HttpSessionBindingEvent httpsessionbindingevent) {
System.out.println("valueBound Name:"+httpsessionbindingevent.getName());
}
public void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent) {
System.out.println("valueUnbound Name:"+httpsessionbindingevent.getName());
}
public void sessionWillPassivate(HttpSessionEvent httpsessionevent) {
System.out.println("sessionWillPassivate "+httpsessionevent.getSource());
}
public void sessionDidActivate(HttpSessionEvent httpsessionevent) {
System.out.println("sessionDidActivate "+httpsessionevent.getSource());
}
}
我们在init页面和destory页面使用request对象添加User对象实例,就可以测试了。
标签:
原文地址:http://blog.csdn.net/erlian1992/article/details/52219885