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是由服务器创建的
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/39494.html