上一节是用Session存字符串数据,这一节是存对象
什么是Session:
服务器会给每一个用户(浏览器)创建一个Session对象
一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在
用户登录之后,整个网站都可以访问 ---->保存用户信息;保存购物车信息 等等..
Session能存字符串,也能存入用户信息
Session和Cookie的区别:
Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
Session把用户的数据写到用户独占Session中,服务器保存(保存重要信息,减少服务器资源浪费)
使用场景:
保存一个登陆用户的信息
购物车信息
在整个网站中经常会使用的数据,我们将它保存在Session中
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 }
原文:https://www.cnblogs.com/WZ-BeiHang/p/12595538.html