一、会话
1.会话:用户打开浏览器,访问web服务器的资源,会话建立,直到有一方断开连接,则会话结束。一次会话中可以包含多次请求和响应。
2.会话跟踪:维护浏览器状态的方法,服务器需要识别多次请求是否来自同一浏览器,以便在同一次会话的多次请求间共享数据。
3.Http协议是无状态的,每次浏览器向服务器请求时,服务器都会将该请求视为新的请求,也就是新的会话,因此我们需要会话跟踪技术来实现会话内数据共享。
4.实现方式:
(1)客户端会话跟踪技术:Cookie
(2)服务器会话跟踪技术:Session
二、Cookie
Cookie:客户端会话技术,将数据保存到客户端,以后每次请求都携带Cookie数据进行访问。
基本使用:
1.发送Cookie
(1)创建Cookie对象,设置数据
(2)发送Cookie到客户端,使用response对象
@WebServlet("/aServlet")
public class aServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.创建Cookie对象
Cookie cookie = new Cookie("username","balalaika");
//2.cookie对象发送至客户端
resp.addCookie(cookie);
}
}
(3)使用request对象的getCookies()方法,获取客户端的cookie对象
@WebServlet("/bServlet")
public class bServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//通过req对象的getCookies()方法,获取客户端的cookie
Cookie[] cookies = req.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
if("username".equals(name)){
String value = cookie.getValue();
System.out.println(name + ":"+value);
break;
}
}
}
}
(4)控制台输出
2.Cookie存活时间
默认情况下,Cookie存储在浏览器内存中,当浏览器关闭,内存释放,则Cookie被销毁;
设置Cookie的存活时间:setMaxAge(int seconds),单位为秒
(1)正数:持久化存储,到时间自动删除;
(2)负数:默认值,当浏览器关闭时,Cookie自动删除;
(3)零:删除对应Cookie;
3.Cookie存储中文
Cookie不能直接存储中文
如需要存储,需要进行转码:
URL编码: URLEncoder.encode(value,"UTF-8");
URL解码:URLDecoder.decode(value,"UTF-8");
@WebServlet("/aServlet")
public class aServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.创建Cookie对象,并存储中文
Cookie cookie = new Cookie("username","张三");
//2.cookie对象发送至客户端
resp.addCookie(cookie);
}
}
控制台输出乱码:
URLEncoder.encode(value,"UTF-8");
@WebServlet("/aServlet")
public class aServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.创建Cookie对象
String value = "张三";
value = URLEncoder.encode(value,"utf-8");
Cookie cookie = new Cookie("username",value);
//2.cookie对象发送至客户端
resp.addCookie(cookie);
}
}
URLDecoder.decode(value,"UTF-8");
@WebServlet("/bServlet")
public class bServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie[] cookies = req.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
if("username".equals(name)){
String value = cookie.getValue();
value = URLDecoder.decode(value,"utf-8");
System.out.println(name+":"+value);
break;
}
}
}
}
4.Cookie原理
Cookie的实现是基于Http协议
响应头:set-cookie
请求头:cookie
三、Session
Session:服务端会话跟踪技术,将数据存储在服务端;
JavaEE提供HttpSession接口,来实现一次会话的多次请求间数据共享功能;
基本使用:
1.获取Session对象
HttpSession session = request.getSession();
@WebServlet("/cServlet")
public class cServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取Session对象
HttpSession session = req.getSession();
System.out.println(session);
}
}
@WebServlet("/dServlet")
public class dServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
System.out.println(session);
}
}
两次获取的Session对象的ID是一致的: 注意:如果关闭浏览器,也就是不在同一次会话内来获取的Session对象是不同的!
2.Session对象功能
存储数据到session域中:void setAttribute(String name,Object o)
根据key,获取值:Object getAttribute(String name)
根据key,删除该键值对:void removeAttribute(String name)
使用setAttribute()存入数据
@WebServlet("/cServlet")
public class cServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
//1.获取Session对象
HttpSession session = req.getSession();
//2.存入数据到session中
session.setAttribute("u1","张三");
resp.getWriter().write("存入session的数据为:" + session);
}
}
使用getAttribute()取出数据
@WebServlet("/aServlet")
public class aServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
HttpSession session = req.getSession();
Object username = session.getAttribute("u1");
resp.getWriter().write("之前存入session的数据:"+ username);
}
}
3.Session原理
Session是基于Cookie实现的
4.服务器重启后,Session中数据是否存在?
钝化:服务器正常关闭后,Tomcat会自动将Session数据写入硬盘的文件中;
活化:再次启动服务器后,会从之前写入的文件中加载数据到Session中;
5.Session销毁
默认情况下,无操作,30分钟自动销毁
<session-config>
<session-timeout>30</session-timeout>
</session-config>
调用Session对象的invalidate()方法销毁;
四、总结
1.Cookie和Session都是来完成一次会话内多次请求间的数据共享的;
2.区别:
(1)存储位置:Cookie是数据存储在客户端,Session是数据存储在服务器端;
(2)安全性:Cookie不安全,Session安全;
(3)数据大小:Cookie最大3KB,Session无大小限制;
(4)存储时间:Cookie可以长期存储,Session默认30分钟;
(5)服务器性能:Cookie不占用服务器资源,Session占用服务器资源;