用Session存對象數據
上一節是用Session存字符串數據,這一節是存對象
什么是Session:
-
服務器會給每一個用戶(瀏覽器)創建一個Session對象
-
一個Session獨占一個瀏覽器,只要瀏覽器沒有關閉,這個Session就存在
-
用戶登錄之后,整個網站都可以訪問 ---->保存用戶信息;保存購物車信息 等等..
-
Session能存字符串,也能存入用戶信息
Session和Cookie的區別:
-
Cookie是把用戶的數據寫給用戶的瀏覽器,瀏覽器保存(可以保存多個)
-
Session把用戶的數據寫到用戶獨占Session中,服務器保存(保存重要信息,減少服務器資源浪費)
-
使用場景:
-
保存一個登陸用戶的信息
-
購物車信息
-
在整個網站中經常會使用的數據,我們將它保存在Session中
以下1-4執行的是:在Demo01類中用Session存對象數據(Person),在Demo02類中用Session讀對象數據(Person)。

1 package com.wang.pojo; 2 3 public class Person { 4 private String name; 5 private int age; 6 public Person(String name, int age){ 7 this.name = name; 8 this.age = age; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setAge(int age) { 20 this.age = age; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 @Override 28 public String toString() { 29 return "Person{" + 30 "name='" + name + '\'' + 31 ", age=" + age + 32 '}'; 33 } 34 }

1 package com.wang.servlet; 2 import com.wang.pojo.Person; 3 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 import java.io.IOException; 10 public class SessionDemo01 extends HttpServlet { 11 @Override 12 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 13 //解決亂碼問題 14 resp.setCharacterEncoding("UTF-8"); 15 req.setCharacterEncoding("UTF-8"); 16 resp.setContentType("text/html;charset=UTF-8"); 17 //得到Session 18 HttpSession session = req.getSession(); 19 //給session中存入東西 20 session.setAttribute("name", new Person("王贊",26)); 21 //獲取Session的ID 22 String sessionId = session.getId(); 23 24 //判斷Session是不是新創建的 25 if(session.isNew()) 26 { 27 resp.getWriter().write("Session創建成功,ID:"+sessionId); 28 } 29 else 30 { 31 resp.getWriter().write("Session已經在服務器中存在,ID:"+sessionId); 32 } 33 } 34 @Override 35 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 36 doGet(req, resp); 37 } 38 }

1 package com.wang.servlet; 2 3 import com.wang.pojo.Person; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import javax.servlet.http.HttpSession; 10 import java.io.IOException; 11 12 public class SessionDemo02 extends HttpServlet { 13 @Override 14 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 15 //解決亂碼問題 16 resp.setCharacterEncoding("UTF-8"); 17 req.setCharacterEncoding("UTF-8"); 18 resp.setContentType("text/html;charset=UTF-8"); 19 //得到Session 20 HttpSession session = req.getSession(); 21 22 Person person = (Person) session.getAttribute("name"); 23 24 System.out.println(person.toString()); 25 } 26 @Override 27 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 28 doGet(req, resp); 29 } 30 }