cookie和session
一、会话跟踪技术
会话:用户打开浏览器,访问web服务器的资源,会话建立,直到有一方断开连接,会话结束。在一次会话中可以包含多次请求和响应。
会话跟踪:一种维护浏览器状态的方法,服务器需要识别多次请求是否来自于同一浏览器,以便在同一次会话的多次请求间共享数据。实现方式有:Cookie(客户端会话跟踪技术)、Session(服务端会话跟踪技术)
二、Cookie
基本使用
Cookie:客户端会话技术,将数据保存到客户端,以后每次请求都携带Cookie数据进行访问.
- 发送cookie
package com.ithecheng.web;
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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
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);
}
}
- 获取cookie
package com.ithecheng.web;
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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
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;
}
}
}
}
cookie原理
cookie是通过http协议实现的,服务器通过响应头set-cookie给浏览器cookie数据,浏览器通过请求头cookie携带cookie数据访问服务器。
cookie使用细节
- cookie存活时间:默认情况下,cookie存储在浏览器内存中,当浏览器关闭,内存释放,cookie被销毁。除此之外,可以使用setMaxAge(int seconds)设置cookie存活时间
package com.ithecheng.web;
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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//发送cookie
//1.创建cookie对象
Cookie cookie = new Cookie("username","zs");
//设置存活时间 一周
cookie.setMaxAge(60*60*24*7); //正数 存储的时间;负数 默认值,存在内存中;零 删除对应cookie
//2.发送cookie,response
response.addCookie(cookie);
}
}
- cookie存储中文
cookie不能直接存储中文,如需要存储,则需要进行转码:URL编码
cookie存储中文时转码:
package com.ithecheng.web;
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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//发送cookie
//1.创建cookie对象
// Cookie cookie = new Cookie("username","zs");
String value = "张三";
value = URLEncoder.encode(value, "UTF-8");
System.out.println("存储数据:"+ value);
Cookie cookie = new Cookie("username",value);
//设置存活时间 一周
cookie.setMaxAge(60*60*24*7); //正数 存储的时间;负数 默认值,存在内存中;零 删除对应cookie
//2.发送cookie,response
response.addCookie(cookie);
}
}
解析cookie的中文:
package com.ithecheng.web;
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.URLDecoder;
import java.net.URLEncoder;
@WebServlet("/bServlet")
public class BServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
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解码
value = URLDecoder.decode(value,"UTF-8");
System.out.println(name + ":" + value);
break;
}
}
}
}
session
基本使用
session:服务端会话跟踪技术,将数据保存到服务端(JavaEE提供HttpSession接口,来实现一次会话多次请求间数据共享)
- session中存储数据
package com.ithecheng.web.session;
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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 存储数据到session中
// 1. 获取session对象
HttpSession session = request.getSession();
// 2. 存储数据
session.setAttribute("username","zs");
}
}
- session中获取数据
package com.ithecheng.web.session;
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 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
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);
}
}
session原理
session实现是基于cookie的