第一章 会话技术简介
1.1 概述
- 会话:一次会话中包含多次请求和响应。
- 一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止。
1.2 功能
- 共享数据。
1.3 方式
- 客户端会话技术:Cookie。
- 服务器端会话技术:Session。
第二章 Cookie
2.1 概念
- 客户端会话技术,将数据保存在客户端。
2.2 快速入门
- 步骤:
- ① 创建Cookie对象,绑定数据。
public Cookie(String name, String value)
- ②发送Cookie对象。
response.addCookie(cookie);
- ③获取Cookie对象。
Cookie[] cookies = request.getCookies();
- 示例:
- CookieDemoServlet.java
package com.sunxiaping.servlet; 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(urlPatterns = "/cookieDemoServlet") public class CookieDemoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 创建Cookie Cookie cookie = new Cookie("msg", "hello"); //2. 发送Cookie对象 response.addCookie(cookie); //重定向到CookieDemo2Servlet response.sendRedirect(request.getContextPath() + "/cookieDemo2Servlet"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
- CookieDemo2Servlet.java
package com.sunxiaping.servlet; 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(urlPatterns = "/cookieDemo2Servlet") public class CookieDemo2Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取Cookie对象 Cookie[] cookies = request.getCookies(); if (null != cookies && cookies.length > 0) { for (Cookie cookie : cookies) { System.out.println("name:" + cookie.getName()); System.out.println("value:" + cookie.getValue()); } } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
2.2 实现原理
- 基于响应头set-cookie和请求头cookie实现的。
2.3 Cookie的细节
2.3.1 一次发送多个Cookie
- 示例:
package com.sunxiaping.servlet; 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(urlPatterns = "/cookieDemoServlet") public class CookieDemoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 创建Cookie Cookie cookie = new Cookie("msg", "hello"); Cookie cookie1 = new Cookie("name","zhangsan"); //2. 发送Cookie对象 response.addCookie(cookie); response.addCookie(cookie1); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
2.3.2 Cookie在浏览器中的保存时间
- 默认情况下,当浏览器关闭后,Cookie中的数据自动销毁。
- 设置Cookie的保存路径以及时间:
public void setPath(String uri) {}
/**
* 正数,将Cookie数据写到硬盘中。持久化存储,表示Cookie的存活时间。
* 负数,默认值
* 0:删除Cookie信息
*/
public void setMaxAge() { }
- 示例:
package com.sunxiaping.servlet; 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(urlPatterns = "/cookieDemoServlet") public class CookieDemoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 创建Cookie Cookie cookie = new Cookie("msg", "hello"); //设置Cookie的生命周期 cookie.setPath(request.getContextPath()); cookie.setMaxAge(60 * 5); //2. 发送Cookie对象 response.addCookie(cookie); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
2.3.3 Cookie中存储中文
- Tomcat8之后Cookie支持存储中文。
- Tomcat7之前需要用URL编码将要存储到Cookie中的中文转码。
2.4 Cookie的特点和作用
- Cookie存储数据在客户端浏览器。
- 浏览器对于单个Cookie的大小有限制(4KB)以及对于同一个域名下的总Cookie数量有限制(20个)。
- 作用:
- ①Cookie一般用于存储少量的不太敏感的数据。
- ②在不登录的情况下,完成服务器对客户身份的识别。
第三章 Session
3.1 概念
- 服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象中。HttpSession。
3.2 快速入门
- 获取HttpSession对象:
HttpSession session = request.getSession();
- 设置属性名和属性值:
public void setAttribute(String name, Object value);
- 根据属性名获取对应的属性值:
public Object getAttribute(String name);
- 移除属性:
public void removeAttribute(String name);
- 示例:
package com.sunxiaping.servlet; 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(name = "SessionDemoServlet", urlPatterns = "/sessionDemoServlet") public class SessionDemoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.setAttribute("msg","hello"); response.sendRedirect(request.getContextPath()+"/sessionDemo2Servlet"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
package com.sunxiaping.servlet; 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(name = "SessionDemo2Servlet", urlPatterns = "/sessionDemo2Servlet") public class SessionDemo2Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); Object msg = session.getAttribute("msg"); System.out.println(msg); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
3.3 Session的特点
- Session用于存储一次会话的多次请求的数据,存储在服务器端。
- Session可以存储任意类型、任意大小的数据。