本文主要是介绍回话跟踪技术,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一. 简介
会话:用户打开浏览器,访问web服务器的资源,会话建立,直到有一方断开连接,会话结束。在一次会话中可以包含多次请求和响应
会话跟踪:一种维护浏览器状态的方法,服务器需要识别多次请求是否来自于同一浏览器,以便在同一次会话的多次请求间共享数据
HTTP协议是无状态的,每次浏览器向服务器请求时, 服务器都会将该请求视为新的请求, 因此我们需要回话跟踪技术来实现回话内数据共享
实现方式:
客户端回话跟踪技术: Cookie
服务端会话跟踪技术: Session
二. Cookie
Cookie: 客户端会话技术, 将数据保存的客户端, 以后每次请求都携带Cookie数据进行访问
Cookie的基本使用
发送Cookie
创建Cookie对象, 设置数据
Cookie cookie = new Cookie("key","value");
发送Cookie到客户端: 使用response对象
response.addCookie(cookie);
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;@WebServlet("/aServlet")
public class AServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//发送cookie//创建cookieCookie cookie = new Cookie("username","guaiwu");//发送到客户端: 使用request对象response.addCookie(cookie);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}
获取Cookie
获取客户端携带的所有Cookie, 使用request对象
Cookie[] cookies = request.getCookies();Cookie[] cookies = request.getCookies();
遍历数组,获取所有Cookie对象
使用Cookie对象方法获取数据
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;@WebServlet("/bServlet")
public class BServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取cookie//获取客户端携带的所有CookieCookie[] cookies = request.getCookies();//遍历数组for (Cookie cookie : cookies) {if(cookie.getName().equals("username")){//使用cookie方法获取数据String name = cookie.getName();String value = cookie.getValue();System.out.println(name+"="+value);}}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}
先访问http://localhost:8080/cookieDemo1/aServlet再访问http://localhost:8080/cookieDemo1/bServlet
Cookie原理
Cookie的实现是基于HTTP协议的
响应头: set-cookie
请求头: cookie
Cookie使用细节
Cookie存活时间
默认情况下,Cookie 存储在浏览器内存中,当浏览器关闭,内存释放,则Cookie被销毁
setMaxAge(int seconds): 设置Cookie存活时间(单位:秒)
正数:将 Cookie写入浏览器所在电脑的硬盘,持久化存储。到时间自动删除
负数:默认值,Cookie在当前浏览器内存中,当浏览器关闭,则 Cookie被销毁
零:删除对应 Cookie
Cookie存储中文
Cookie不能直接存储中文
如果要存储中文必须进行转码: URL编码
代码
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;@WebServlet("/aServlet")
public class AServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//发送cookie//创建cookie//Cookie cookie = new Cookie("username","guaiwu");String value = "怪物";//URL编码value = URLEncoder.encode(value, "utf-8");Cookie cookie = new Cookie("username",value);//发送到客户端: 使用request对象response.addCookie(cookie);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}
javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;@WebServlet("/bServlet")
public class BServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取cookie//获取客户端携带的所有CookieCookie[] cookies = request.getCookies();//遍历数组for (Cookie cookie : cookies) {if(cookie.getName().equals("username")){//使用cookie方法获取数据String name = cookie.getName();String value = cookie.getValue();//URL解码value = URLDecoder.decode(value, "utf-8");System.out.println(name+"="+value);break;}}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}
结果展示
三. Session
服务端会话跟踪技术:将数据保存到服务端
JavaEE 提供 HttpSession接口,来实现一次会话的多次请求间数据共享功能
Session的基本使用
获取Session对象
HttpSession session = request.getSession();
Session对象功能
void setAttribute(String name, Object o): 存储数据到 session 域中
Object getAttribute(String name): 根据 key,获取值
void removeAttribute(String name): 根据 key,删除该键值对
代码
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;@WebServlet("/demo1")
public class SessionDemo1 extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取Session对象HttpSession session = request.getSession();//将数据存到session域中session.setAttribute("username","guaiwu");}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;@WebServlet("/demo2")
public class SessionDemo2 extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取session对象HttpSession session = request.getSession();//调用方法,获取值Object username = session.getAttribute("username");//如果可以打印出guaiwu说明实现了在一次回话的多次请求数据共享System.out.println(username);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}
效果展示
Session原理
Session是基于Cookie实现的
http://localhost:8080/cookieDemo1/demo1
http://localhost:8080/cookieDemo1/demo2
Session使用细节
Session 钝化、活化:
服务器重启后,Session中的数据是否还在?
钝化:在服务器正常关闭后, Tomcat会自动将 Session数据写入硬盘的文件中
活化:再次启动服务器后,从文件中加载数据到Session中
Session销毁
默认情况下,无操作,30分钟后自动销毁
在web.xml下设置
<session-config>
<!--单位:分钟-->
<session-timeout>50</session-timeout>
</session-config>
调用Session对象的invalidate()方法
Cookie和Session的区别
存储位置: Cookie 是将数据存储在客户端,Session 将数据存储在服务端
安全性: Cookie 不安全,Session 安全
数据大小: Cookie 最大3KB,Session 无大小限制
存储时间: Cookie 可以长期存储,Session 默认30分钟
服务器性能: Cookie 不占服务器资源,Session 占用服务器资源
这篇关于回话跟踪技术的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!