Java中Session的详解
目录
- 1.什么是session:
- 2.session的经典应用场景:
- 3.session中常用的方法
- 4.举例
- 5.session与cookie的区别
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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//解决中文乱码resp.setHeader("content-type","text/html; charset=UTF-8"); //得到sessionHttpSession 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); }}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp); } }
步骤3:配置XML文件
SessionDemo01com.xiaoma.servlet.SessionDemo01SessionDemo01/s1
例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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//解决中文乱码resp.setHeader("content-type","text/html; charset=UTF-8"); //得到sessionHttpSession 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); }}@Overrideprotected 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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//解决中文乱码resp.setHeader("content-type","text/html; charset=UTF-8"); //得到sessionHttpSession session = req.getSession(); String name=(String) session.getAttribute("name"); resp.getWriter().write(name); }@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp); } }
步骤3:配置xml文件
SessionDemo02com.xiaoma.servlet.SessionDemo02SessionDemo02/s2
例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; }@Overridepublic 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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//解决中文乱码resp.setHeader("content-type","text/html; charset=UTF-8"); //得到sessionHttpSession 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); }}@Overrideprotected 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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取session中的Person对象//解决中文乱码resp.setHeader("content-type","text/html; charset=UTF-8"); //得到sessionHttpSession session = req.getSession(); Person person=(Person) session.getAttribute("name"); resp.getWriter().write(person.toString()); }@Overrideprotected 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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//得到sessionHttpSession session = req.getSession(); //手动注销sessionsession.removeAttribute("name"); session.invalidate(); }@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp); }}
第二种,通过xml文件的方法,这种方式是时session自动过期
1
5.session与cookie的区别 ·(1)cookie是吧用户的数据写给浏览器,浏览器保存(可以保存多个)
【Java中Session的详解】·(2)session是把用户的数据写给用户独占的session中,服务器保存(保存重要信息,避免浪费服务器资源)
·(3)session是由服务器创建的
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- Shell-Bash变量与运算符
- JS中的各种宽高度定义及其应用
- 2021-02-17|2021-02-17 小儿按摩膻中穴-舒缓咳嗽
- 深入理解Go之generate
- 异地恋中,逐渐适应一个人到底意味着什么()
- 我眼中的佛系经纪人
- 《魔法科高中的劣等生》第26卷(Invasion篇)发售
- “成长”读书社群招募