初学javaee—Session②

初学javaee

Session②

Session的持久化

• 当一个Session开始时,Servlet容器会为Session创建一个HttpSession对象。
• Servlet容器在某些情况下把这些HttpSession对象从内存中转移到文件系统或数据库中, 在需要访问HttpSession信息时再把它们加载到内存中。
session的持久化
假定有一万个人同时在访问某个Web应用,Servlet容器中会生成一万个HttpSession对象。如果把这些对象都一直存放在内存中,将消耗大量的内存资源。因此可以把处于不活动状态的HttpSession对象转移到文件系统或数据库中,这样可以提高对内存资源的利用率。
Session的持久化由Session Manager来管理,Tomcat服务器提供了两个实现类:
org.apache.catalina.session.StandardManager
org.apache.catalina.session.PersistentManager
StandardManager是Tomcat默认的Session Manager。
当Tomcat服务器关闭、重启,或者Web应用被重新加载时,会对内存中的HttpSession对象进行持久化,把它们保存到文件系统中:
<CATALINA_HOME>/work/Catalina/hostname/applicationname/SESSIONS.ser
PersistentManager把Session对象保存到SessionStore中,提供了比StandardManager更灵活的Session管理功能:
•Tomcat服务器关闭、重启,或Web应用被重新加载时,对内存中的HttpSession对象进行持久化,保存到Session Store中。
•容错功能, 服务器关闭,及时把Session备份到Session Store中
• 控制内存中的Session数目,将部分Session转移到Session Stor

Session的监听

Servlet API中定义了监听会话的监听器Listener接口
• 可以监听客户端的请求、服务端的操作。
• 通过监听器,自动激发一些操作,如监听在线用户数量,当增加一个HttpSession时,在线人数加1。
• 编写监听器类实现相应的接口
• 编写实现类后,在web.xml文件中通过属性进行配置

<web-app>
<listener>
<listener-class>mypack.MySessionLifeListener</listener-class>
</listener>
</web-app>
  1. HttpSessionListener 监听HttpSession的操作
    当创建一个Session时,sessionCreated(HttpSessionEvent se)
    当销毁一个Session时,sessionDestroyed (HttpSessionEvent se)
    计算当前有多少个在线用户
    2.HttpSessionAttributeListener 监听HttpSession中的属性的操作
    Session增加一个属性,attributeAdded(HttpSessionBindingEvent se)
    Session删除一个属性,attributeRemoved (HttpSessionBindingEvent se)
    Session属性被重新设置时,attributeReplaced (HttpSessionBindingEvent se)

实例

实例-统计在线用户人数
• 利用HttpSessionListener统计在线用户人数
• 一个在线用户对应一个会话,Web应用的当前所有会话数目就是在线用户数目
• 用户数Counter属性存放在Web应用范围内,Servlet容器创建一个会话,Counter值加1;Servlet容器销毁一个会话,Counter值减1
OnlineCounterListener.java

package cookieandsession;
//这里的cookieandsession是存放OnlineCounterListener.java的包
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineCounterListener implements HttpSessionListener {
	@SuppressWarnings("deprecation")
	public void sessionCreated(HttpSessionEvent event) {
		HttpSession session=event.getSession();
		ServletContext context=session.getServletContext();
		Integer counter=(Integer)context.getAttribute("counter");
		if(counter==null)
			counter=new Integer(1);
		else
			counter=new Integer(counter+1);
		context.setAttribute("counter", counter);
		session.setMaxInactiveInterval(60);
		System.out.println("A new session is created.");
	}
	@SuppressWarnings("deprecation")
	public void sessionDestroyed(HttpSessionEvent event) {
		HttpSession session=event.getSession();
		ServletContext context=session.getServletContext();
		Integer counter=(Integer)context.getAttribute("counter");
		if(counter==null)
			return;
		else
			counter=new Integer(counter-1);
		System.out.println("A new session is to be destroyed.");
	}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <listener>
<listener-class>cookieandsession.OnlineCounterListener</listener-class>
</listener>
</web-app>

mailcheck2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%
Integer counter=(Integer)application.getAttribute("counter");
if(counter!=null){
	%>
	当前在线人数为:<%=counter %><br>
<%} %>
<body>
</body>
</html>

onlinecounter.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Integer hitsCount = (Integer) application.getAttribute("hitCounter");
if(hitsCount == null || hitsCount == 0) {
/*第一次访问 */
out.println("Welcome to my website!");
hitsCount = 1;
}else{
/* 返回访问值 */
out.println("Welcome back!");
hitsCount += 1;
}
application.setAttribute("hitCounter", hitsCount);
%>
<p>Total number of visits:
<%=hitsCount%></p>
</body>
</html>

结果截图
运行mailcheck2.jsp
mailcheck2.jsp
运行onlinecounter.jsp
onlineCounter.jsp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值