初學javaee
Session②
Session的持久化
• 當一個Session開始時,Servlet容器會為Session建立一個HttpSession對象。
• Servlet容器在某些情況下把這些HttpSession對象從記憶體中轉移到檔案系統或資料庫中, 在需要通路HttpSession資訊時再把它們加載到記憶體中。

假定有一萬個人同時在通路某個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>
-
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
運作onlinecounter.jsp