您好,登錄后才能下訂單哦!
Java中Session的介紹以及用法是怎樣的,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
服務器會給每一個用戶(瀏覽器)創建一個session對象
一個session獨占一個瀏覽器,只要瀏覽器不關閉,這個session就一直存在
session稱為會話控制,存儲用戶會話所需的屬性和配置信息,這樣,用戶在應用程序的web頁面跳轉時,存儲在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的
例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>
·(1)cookie是吧用戶的數據寫給瀏覽器,瀏覽器保存(可以保存多個)
·(2)session是把用戶的數據寫給用戶獨占的session中,服務器保存(保存重要信息,避免浪費服務器資源)
·(3)session是由服務器創建的
關于Java中Session的介紹以及用法是怎樣的問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。