Session 會話
一、什么是 Session 會話?
1、Session 就是一個介面(HttpSession), 2、Session 就是會話,它是用來維護一個客戶端和服務器之間關聯的一種技術, 3、每個客戶端都有自己的一個 Session 會話, 4、Session 會話中,我們經常用來保存用戶登錄之后的資訊,二、如何創建 Session 和獲取(id 號,是否為新)
對于創建和獲取Session,它們的API是一樣的,
request.getSession()
第一次呼叫是:創建 Session 會話 之后呼叫都是:獲取前面創建好的 Session 會話物件,isNew(); 判斷到底是不是剛創建出來的(新的)
true 表示剛創建 false 表示獲取之前創建 每個會話都有一個身份證號,也就是 ID 值,而且這個 ID 是唯一的, getId() 得到 Session 的會話 id 值,SessionServlet程式:
public class SessionServlet extends BaseServlet { protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //創建Session HttpSession session = req.getSession(); //判斷Session是否為新創建的 boolean isNewSession = session.isNew(); //獲取Session會話的唯一ID String sessionId = session.getId(); resp.getWriter().write("當前Session物件是否為新創建的:" + isNewSession + "<br/>"); resp.getWriter().write("當前Session物件的唯一ID值為:" + sessionId); } }
session.html頁面:

三、Session 域資料的存取
SessionServlet程式:
/** * 往Session中保存資料 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getSession().setAttribute("key1", "value1"); resp.getWriter().write("已經往Session中保存了資料"); } /** * 獲取Session域中的資料 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Object attribute = req.getSession().getAttribute("key1"); resp.getWriter().write("從Session中獲取了key1的資料是:" + attribute); }
session.html頁面:

四、Session 生命周期控制
4.1、Session的默認超時時長
public int getMaxInactiveInterval():獲取 Session 的超時時間(以秒為單位)SessionServlet程式:
/** * Session的默認超時時長 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); int defaultOutTime = session.getMaxInactiveInterval(); resp.getWriter().write("Session會話的默認超時時長為:" + defaultOutTime); }
session.html頁面:

瀏覽器訪問效果:

說明:
Session 默認的超時時間長為 30 分鐘, 因為在 Tomcat 服務器的組態檔 web.xml中默認有以下的配置,它就表示配置了當前 Tomcat 服務器下所有的 Session超時配置默認時長為:30 分鐘,
如果說,你希望你的 web 工程,默認的 Session 的超時時長為其他時長,你可以在你自己的 web.xml 組態檔中做以上相同的配置,就可以修改你的 web 工程所有 Seession 的默認超時時長,具體操作如下: 在當前工程的web.xml檔案中做如下配置:
4.2、修改個別Session的超時時長
如果你想只修改個別 Session 的超時時長,就可以使用上面的 API,setMaxInactiveInterval(int interval)來進行單獨的設定, session.setMaxInactiveInterval(int interval):設定 Session 的超時時間(以秒為單位),超過指定的時長,Session就會被銷毀, 值為正數的時候,設定 Session 的超時時長, 負數表示永不超時(極少使用)SessionServlet程式:
/** * 設定Session3秒后超時 * @param req * @param resp * @throws ServletException * @throws IOException */ protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //獲取Session物件 HttpSession session = req.getSession(); //設定當前Session3秒后超時 session.setMaxInactiveInterval(3); resp.getWriter().write("當前Session已經設定為3秒后超時"); }
session.html頁面:

瀏覽器效果:

針對于上面的情況,我們需要對Session超時的概念做一個介紹:

4.3、Session馬上超時介紹
public void invalidate() :讓當前 Session 會話馬上超時無效,SessionServlet程式:
protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //先獲取Session物件 HttpSession session = req.getSession(); //讓Session會話馬上無效 session.invalidate(); resp.getWriter().write("Session已經設定為超時(無效)"); }
session.html頁面:

五、瀏覽器和 Session 之間關聯的技術內幕

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/220690.html
標籤:其他
