1.1 基本概念

-
Session本意為"會話"的含義,是用來維護一個客戶端和服務器關聯的一種技術。瀏覽器訪問服務器時,服務器會為每一個瀏覽器都在服務器端的內存中分配一個空間,用於創建一個Session對象。
-
該對象有一個id屬性且該值唯一,稱為SessionId,並且服務器會將這個SessionId以Cookie方式發送給瀏覽器存儲。
-
瀏覽器再次訪問服務器時會將SessionId發送給服務器,服務器可以依據SessionId查找相對應的Session對象。
1.2 相關的方法
- 使用javax.servlet.http.HttpServletRequest接口的成員方法實現Session的獲取。
| 方法聲明 | 作用 |
|---|---|
| HttpSession getSession( ) | 返回此請求關聯的當前Session,若此請求沒有則創建一個 |
- 使用javax.servlet.http.HttpSession接口的成員方法實現判斷和獲取。
| 方法聲明 | 作用 |
|---|---|
| boolean isNew( ) | 判斷是否為新創建的Session |
代碼示例
package cn.guardwhy.demo01;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "SessionServlet1", urlPatterns = "/session1")
public class SessionServlet1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.獲取Session對象,得到會話域
HttpSession session = request.getSession();
// 2.判斷Session對象是否為新建的對象
System.out.println(session.isNew() ? "新創建的Session對象": "已有的Session對象");
// 3.獲取編號並且打印
String id = session.getId();
System.out.println("獲取到的Session編號:" + id);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
執行結果

- 使用javax.servlet.http.HttpSession接口的成員方法實現屬性的管理。
| 方法聲明 | 作用 |
|---|---|
| Object getAttribute(String name) | 返回在此會話中用指定名稱綁定的對象,如果沒有對象在該名稱下綁定,則返回空值。 |
| void setAttribute(String name, Object value) | 使用指定的名稱將對象綁定到此會話。 |
| void removeAttribute(String name) | 從此會話中刪除與指定名稱綁定的對象。 |
代碼示例
package cn.guardwhy.demo01;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "SessionServlet2", urlPatterns = "/session2")
public class SessionServlet2 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.獲取Session對象,得到會話域
HttpSession session = request.getSession();
// 2.會話域中存入屬性名和屬性值
session.setAttribute("username", "guardwhy");
// 3.從會話域中取出對應的屬性值
System.out.println("屬性值:" + session.getAttribute("username"));
// 4.刪除指定的屬性名
session.removeAttribute("username");
// 5.從會話域中取出對應的屬性值
System.out.println("屬性值:" + session.getAttribute("username"));
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
執行結果

1.3 Session的生命周期
- 為了節省服務器內存空間資源,服務器會將空閑時間過長的Session對象自動清除掉,服務器默認的超時限制一般是30分鍾。
- 使用javax.servlet.http.HttpSession接口的成員方法實現失效實現的獲取和設置。
| 方法聲明 | 作用 |
|---|---|
| int getMaxInactiveInterval( ) | 獲取失效時間。 |
| void setMaxInactiveInterval(int interval) | 設置失效時間 |
代碼示例
package cn.guardwhy.demo01;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "SessionServlet3", urlPatterns = "/session3")
public class SessionServlet3 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.獲取Session對象,得到會話域
HttpSession session = request.getSession();
// 2.獲取對象的默認失效時間
int maxInactiveInterval = session.getMaxInactiveInterval();
System.out.println("獲取到失效時間:" + maxInactiveInterval); // 1800
// 3.修改實現時間
session.setMaxInactiveInterval(1000);
maxInactiveInterval = session.getMaxInactiveInterval();
System.out.println("獲取失效時間:" + maxInactiveInterval); // 1000
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
執行結果

- 配置web.xml文件修改失效時間
<session-config>
<!--分鍾-->
<session-timeout>10</session-timeout>
</session-config>
1.4 Session的特點
- 數據比較安全。能夠保存的數據類型豐富,而Cookie只能保存字符串。
- 能夠保存更多的數據,而Cookie大約保存4KB。
- 數據保存在服務器端會占用服務器的內存空間,如果存儲信息過多、用戶量過大,會嚴重影響服務器的性能。
1.5 Session和cookie的區別
- Cookie是把用戶的數據寫給用戶的瀏覽器,瀏覽器保存 (可以保存多個),Session對象由服務創建。
- Session把用戶的數據寫到用戶獨占Session中,服務器端保存 (保存重要的信息,減少服務器資源的浪費)
