会话技术产生的原因
浏览器和服务器之间使用的是HTTP请求来进行数据传输HTTP协议是无状态的,每次浏览器向服务器请求时,服务器都会将该请求视为新的请求HTTP协议设计成无状态的目的是让每次请求之间相互独立,互不影响请求与请求之间独立后,就无法实现多次请求之间的数据共享
Cookie与Session区别:
存储位置:Cookie 是将数据存储在客户端,Session 将数据存储在服务端
安全性:Cookie不安全,Session安全
数据大小:Cookie最大3KB,Session无大小限制
存储时间:Cookie可以通过setMaxAge()长期存储,Session默认30分钟
服务器性能:Cookie不占服务器资源,Session占用服务器资源
Cookie是用来保证用户在未登录情况下的身份识别
Session是用来保存用户登录后的数据
一、客户端会话跟踪技术:Cookie
(1)Cookie:
客户端会话技术,将数据保存到客户端,以后每次请求都携带Cookie数据进行访问。
(2)基本过程:
浏览器发送请求A服务端ServletA在处理的过程中可以创建一个Cookie对象,响应数据时会把Cookie对象响应给浏览器浏览器接收到响应数据,会把Cookie对象中的数据存储在浏览器内存中,此时浏览器和服务端就建立了一次会话在同一次会话中浏览器再次发送HTTP请求2给服务端ServletB,浏览器会携带Cookie对象中的所有数据服务端ServletB接收到请求和数据后,就可以获取到存储在Cookie对象中的数据
(3)Cookie的使用
1 发送Cookie
创建Cookie对象,并设置数据
Cookie cookie = new Cookie("key","value");
发送Cookie到客户端:使用response对象
response.addCookie(cookie);
@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//发送Cookie
//1. 创建Cookie对象
Cookie cookie = new Cookie("username","zs");
//2. 发送Cookie,response
response.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
2 获取Cookie
获取客户端携带的所有Cookie,使用request对象
Cookie[] cookies = request.getCookies();
使用Cookie对象方法获取数据
cookie.getName();
cookie.getValue();
@WebServlet("/bServlet")
public class BServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取Cookie
//1. 获取Cookie数组
Cookie[] cookies = request.getCookies();
//2. 遍历数组
for (Cookie cookie : cookies) {
//3. 获取数据
String name = cookie.getName();
if("username".equals(name)){
String value = cookie.getValue();
System.out.println(name+":"+value);
break;
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
(4)Cookie实现细节
AServlet给前端发送Cookie
Tomcat发现后端要返回的是一个Cookie对象之后,Tomcat就会在响应头中添加一行数据Set-Cookie:username=zs,浏览器获取到响应结果后,从响应头中就可以获取到Set-Cookie对应值username=zs,并将数据存储在浏览器的内存中
BServlet从request中获取Cookie的功能
浏览器再次发送请求给BServlet的时候,浏览器会自动在请求头中添加Cookie: username=zs发送给服务端BServlet,Request对象会把请求头中cookie对应的值封装成一个个Cookie对象,最终形成一个数组
(5)Cookie存活时间(使用setMaxAge)
默认情况,浏览器一关,Cookie就会从浏览器内存中删除,对于记住我(下次再登陆的时候,用户名和密码就会被自动填充,不需要再重新输入登录)功能就无法实现
设置Cookie存活时间
setMaxAge(int seconds)
参数值为:
正数:将Cookie写入浏览器所在电脑的硬盘,持久化存储。到时间自动删除
负数:默认值,Cookie在当前浏览器内存中,当浏览器关闭,则Cookie被销毁
零:删除对应Cookie
@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//发送Cookie
//1. 创建Cookie对象
Cookie cookie = new Cookie("username","zs");
//设置存活时间 ,1周 7天
cookie.setMaxAge(60*60*24*7); //易阅读,需程序计算
//cookie.setMaxAge(604800); //不易阅读(可以使用注解弥补),程序少进行一次计算
//2. 发送Cookie,response
response.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
(6)Cookie存储中文(URL编码和解码)
Cookie不能直接存储中文
实现存储中文:
在AServlet中对中文进行URL编码,采用URLEncoder.encode(),将编码后的值存入Cookie中在BServlet中获取Cookie中的值,获取的值为URL编码后的值将获取的值在进行URL解码,采用URLDecoder.decode(),就可以获取到对应的中文值
代码实现:
在AServlet中对中文进行URL编码
@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//发送Cookie
String value = "张三";
//对中文进行URL编码
value = URLEncoder.encode(value, "UTF-8");
System.out.println("存储数据:"+value);
//将编码后的值存入Cookie中
Cookie cookie = new Cookie("username",value);
//设置存活时间 ,1周 7天
cookie.setMaxAge(60*60*24*7);
//2. 发送Cookie,response
response.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
在BServlet中获取值,并对值进行解码
@WebServlet("/bServlet")
public class BServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取Cookie
//1. 获取Cookie数组
Cookie[] cookies = request.getCookies();
//2. 遍历数组
for (Cookie cookie : cookies) {
//3. 获取数据
String name = cookie.getName();
if("username".equals(name)){
String value = cookie.getValue();//获取的是URL编码后的值 %E5%BC%A0%E4%B8%89
//URL解码
value = URLDecoder.decode(value,"UTF-8");
System.out.println(name+":"+value);//value解码后为 张三
break;
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
二、服务端会话跟踪技术:Session
(1)概念
Session:服务端会话跟踪技术,将数据保存到服务端。
Session是存储在服务端而Cookie是存储在客户端存储在客户端的数据容易被窃取和截获,存在很多不安全的因素存储在服务端的数据相比于客户端来说就更安全
工作流程:
在服务端的AServlet获取一个Session对象,把数据存入其中在服务端的BServlet获取到相同的Session对象,从中取出数据
(2)Session的使用
在JavaEE中提供了HttpSession接口,来实现一次会话的多次请求之间数据共享功能
获取Session对象,使用的是request对象
HttpSession session = request.getSession();
存储数据到 session 域中
void setAttribute(String name, Object o)
根据 key,获取值
Object getAttribute(String name)
根据 key,删除该键值对
void removeAttribute(String name)
SessionDemo1:获取Session对象、存储数据
@WebServlet("/demo1")
public class SessionDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//存储到Session中
//1. 获取Session对象
HttpSession session = request.getSession();
//2. 存储数据
session.setAttribute("username","zs");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
SessionDemo2:获取Session对象、获取数据
@WebServlet("/demo2")
public class SessionDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取数据,从session中
//1. 获取Session对象
HttpSession session = request.getSession();
//2. 获取数据
Object username = session.getAttribute("username");
System.out.println(username);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
(3)Session原理分析
Session是基于Cookie来实现的
demo1在第一次获取session对象的时候,session对象会有一个唯一的标识,假如是id:10Tomcat服务器把session的唯一标识id:10当做一个cookie,添加Set-Cookie:JESSIONID=10到响应头中,并响应给浏览器浏览器在同一会话中访问demo2的时候,会把cookie中的数据按照cookie: JESSIONID=10的格式添加到请求头中并发送给服务器Tomcat从请求头中就读取cookie中的JSESSIONID值为10,然后就会到服务器内存中寻找id:10的session对象
注意:
在一台电脑上演示的时候,如果是相同的浏览器必须要把浏览器全部关掉重新打开,才算新开的一个浏览器。当然也可以使用不同的浏览器进行测试,就不需要把之前的浏览器全部关闭关闭打开浏览器后,因为浏览器的cookie已被销毁,所以就没有JESSIONID的数据,服务端获取到的session就是一个全新的session对象
(4)Session钝化与活化
使用命令行的方式来启动和停止Tomcat服务器:
启动:进入到项目pom.xml所在目录,执行tomcat7:run
停止:在启动的命令行界面,输入ctrl+c
(1)先启动Tomcat服务器
(2)访问http://localhost:8080/cookie-demo/demo1将数据存入session中
(3)正确停止Tomcat服务器
(4)再次重新启动Tomcat服务器
(5)访问http://localhost:8080/cookie-demo/demo2 查看是否能获取到session中的数据
钝化:在服务器正常关闭后,Tomcat会自动将Session数据写入硬盘的文件中
钝化的数据路径为:项目目录\target\tomcat\work\Tomcat\localhost\项目名称\SESSIONS.ser
活化:再次启动服务器后,从文件中加载数据到Session中
数据加载到Session中后,路径中的SESSIONS.ser文件会被删除掉
注意:
session数据存储在服务端,服务器重启后,session数据会被保存
浏览器被关闭启动后,重新建立的连接就已经是一个全新的会话,获取的session数据也是一个新的对象
session的数据要想共享,浏览器不能关闭,所以session数据不能长期保存数据
cookie是存储在客户端,是可以长期保存
(5)Session销毁
默认情况下,无操作,30分钟自动销毁,对于这个失效时间,是可以通过配置进行修改的
在项目web.xml中
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
调用Session对象的invalidate()进行销毁
@WebServlet("/demo2")
public class SessionDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取数据,从session中
//1. 获取Session对象
HttpSession session = request.getSession();
System.out.println(session);
// 销毁
session.invalidate();
//2. 获取数据
Object username = session.getAttribute("username");
System.out.println(username);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}