Java中Session的详解
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是由服务器创建的
推荐阅读