Java中Session的詳解
1.什麼是session:
服務器會給每一個用戶(瀏覽器)創建一個session對象
一個session獨占一個瀏覽器,隻要瀏覽器不關閉,這個session就一直存在
session稱為會話控制,存儲用戶會話所需的屬性和配置信息,這樣,用戶在應用程序的web頁面跳轉時,存儲在session對象中的用戶信息不會丟擲,而是在整個用戶會話中一直保持下去
2.session的經典應用場景:
一個用戶登陸後,訪問該網站的其他網頁時,一直處於登錄狀態
保存購物車信息等等
3.session中常用的方法
getId():獲取session的唯一標識
getServletContext():代表整個web服務
getAttribute(String):獲取session的節點,比如你在其中一個Servlet中通過setAttribute(String,Object)配置瞭相關的session信息,然後通過另一個Servlet獲取這個Servlet中的session信息,就要用到getAttribute(String)這個方法
setAttribute(String,Object):設置session節點
removeAttribute(String):一出一個session節點
isNew():判斷一個session是否是一個新的session
invalidate():註銷session的
4.舉例
例1:創建一個session並獲取到session的ID,判斷這個session是否是新的session
步驟1:新建一個maven項目,使用webapp模板
步驟2:在Java文件夾下創建一個com.xiaoma.servlet.SessionDemo01的類,編寫servlet類文件
package com.xiaoma.servlet; import com.xiaoma.pojo.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解決中文亂碼 resp.setHeader("content-type","text/html;charset=UTF-8"); //得到session HttpSession session = req.getSession(); //通過setAttribute方法向Session中存放信息 session.setAttribute("name","小馬"); //獲取session相關信息,包括id,是否是新的session等 String sessionId = session.getId(); if(session.isNew()) { resp.getWriter().write("創建Session成功,ID為:" + sessionId); }else{ resp.getWriter().write("Session已經存在瞭,ID為:" + sessionId); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
步驟3:配置XML文件
<servlet> <servlet-name>SessionDemo01</servlet-name> <servlet-class>com.xiaoma.servlet.SessionDemo01</servlet-class> </servlet> <servlet-mapping> <servlet-name>SessionDemo01</servlet-name> <url-pattern>/s1</url-pattern> </servlet-mapping>
例2:從一個servlet中讀取另一個servlet中的session信息
步驟1:在Java文件夾下創建一個com.xiaoma.servlet.SessionDemo01的類,編寫servlet類文件
package com.xiaoma.servlet; import com.xiaoma.pojo.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解決中文亂碼 resp.setHeader("content-type","text/html;charset=UTF-8"); //得到session HttpSession session = req.getSession(); //通過setAttribute方法向Session中存放信息 session.setAttribute("name","小馬"); //獲取session相關信息,包括id,是否是新的session等 String sessionId = session.getId(); if(session.isNew()) { resp.getWriter().write("創建Session成功,ID為:" + sessionId); }else{ resp.getWriter().write("Session已經存在瞭,ID為:" + sessionId); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
步驟2:在Java文件夾下創建一個com.xiaoma.servlet.SessionDemo02的類,編寫另一個用來讀取的servlet類,通過SessionDemo02來讀取SessionDemo01中的session信息
package com.xiaoma.servlet; import com.xiaoma.pojo.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo02 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解決中文亂碼 resp.setHeader("content-type","text/html;charset=UTF-8"); //得到session HttpSession session = req.getSession(); String name=(String) session.getAttribute("name"); resp.getWriter().write(name); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
步驟3:配置xml文件
<servlet> <servlet-name>SessionDemo02</servlet-name> <servlet-class>com.xiaoma.servlet.SessionDemo02</servlet-class> </servlet> <servlet-mapping> <servlet-name>SessionDemo02</servlet-name> <url-pattern>/s2</url-pattern> </servlet-mapping>
例3:session中還可以存放對象信息
步驟1:在Java文件夾下創建一個com.xiaoma.pojo.Person的類,編寫我們的實體類
package com.xiaoma.pojo; public class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
步驟2:在Java文件夾下創建一個com.xiaoma.servlet.SessionDemo01的類,編寫servlet類文件
package com.xiaoma.servlet; import com.xiaoma.pojo.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解決中文亂碼 resp.setHeader("content-type","text/html;charset=UTF-8"); //得到session HttpSession session = req.getSession(); //通過setAttribute方法向Session中存放信息 session.setAttribute("name",new Person("小馬",18)); //獲取session相關信息,包括id,是否是新的session等 String sessionId = session.getId(); if(session.isNew()) { resp.getWriter().write("創建Session成功,ID為:" + sessionId); }else{ resp.getWriter().write("Session已經存在瞭,ID為:" + sessionId); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
步驟3:在Java文件夾下創建一個com.xiaoma.servlet.SessionDemo02的類,編寫另一個servlet類文件,用來讀取SessionDemo01中的Session對象信息
package com.xiaoma.servlet; import com.xiaoma.pojo.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo02 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //獲取session中的Person對象 //解決中文亂碼 resp.setHeader("content-type","text/html;charset=UTF-8"); //得到session HttpSession session = req.getSession(); Person person=(Person) session.getAttribute("name"); resp.getWriter().write(person.toString()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
步驟4:編寫xml文件,與上同理
例4:註銷session(註銷session有兩種方式,一種是通過removeAttribute(String)方法,另一種是通過xml配置文件)
第一種,通過removeAttribute(String)方法,這種方式是使session手動過期
package com.xiaoma.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class SessionDemo03 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //得到session HttpSession session = req.getSession(); //手動註銷session session.removeAttribute("name"); session.invalidate(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
第二種,通過xml文件的方法,這種方式是時session自動過期
<!--設置session的存活時間--> <session-config> <!--以分鐘為單位--> <session-timeout>1</session-timeout> </session-config>
5.session與cookie的區別
·(1)cookie是吧用戶的數據寫給瀏覽器,瀏覽器保存(可以保存多個)
·(2)session是把用戶的數據寫給用戶獨占的session中,服務器保存(保存重要信息,避免浪費服務器資源)
·(3)session是由服務器創建的
推薦閱讀:
- 淺談Servlet的Cookie和Session機制
- Java中Servlet的生命周期詳解
- Java Session會話追蹤原理深入分析
- JavaWeb中請求轉發和請求重定向的區別以及使用
- SpringBoot中使用Servlet的兩種方式小結