会话技术--cookie
Posted lxy522
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了会话技术--cookie相关的知识,希望对你有一定的参考价值。
1、会话:一次会话中包含多次请求和响应。
一次会话:浏览器第一次给服务器发送资源,会话建立,直到有一方断开为止。
2、功能:在一次会话的范围内的多次请求间,共享数据。
3、方式:客户端会话技术:cookie; 服务器端会话技术:session。
1、概念:客户端会话技术,将数据保存到客户端。
2、快速入门:
使用步骤:
1、创建cookie对象,绑定数据
new Cookie("user", "root");
2、发送cookie对象
response.addCookie(cookie1);
3、获取cookie,拿到数据
Cookie[] cookies = request.getCookies();
3、实现原理
基于响应头set-cookie和请求头cookie实现
/**
* @program: day16_cookie&&session
* @description:默认情况下,浏览器关闭后,cookie数据被销毁
* @author: lixy
* @create: 2020-05-23 19:18
**/
@WebServlet(name = "CookieDemo1", urlPatterns = {"/cookieDemo1"})
public class CookieDemo1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie1 = new Cookie("user", "李逸");
Cookie cookie2 = new Cookie("password", "root");
//设置cookie1的存活时间
cookie1.setMaxAge(30);//将cookie持久化到硬盘,30秒后会自动删除这个cookie
//设置cookie的获取范围。默认情况下,设置当前的虚拟目录
cookie1.setPath("/");
//设置默认方式
cookie2.setMaxAge(-1);
response.addCookie(cookie1);
response.addCookie(cookie2);
?
}
?
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
@WebServlet(name = "CookieDemo2", urlPatterns = {"/ookieDemo2"})
public class CookieDemo2 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String contextPath = request.getContextPath();
?
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println(name+":"+value);
}
}
?
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
@WebServlet(name = "CookieTest", urlPatterns = {"/cookieTest"})
public class CookieTest extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String contextPath = request.getContextPath();
String user = request.getParameter("name");
String password = request.getParameter("password");
Cookie[] cookies = request.getCookies();
boolean flag = false;
if (cookies != null && cookies.length>0){
for (Cookie cookie : cookies) {
String name = cookie.getName();
if ("lastTime".equals(name)){
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String value = cookie.getValue();
//url解码
value = URLDecoder.decode(value, "UTF-8");
System.out.println("编码前:"+time);
//url编码
time = URLEncoder.encode(time, "utf-8");
System.out.println("编码后:"+time);
cookie.setValue(time);
cookie.setMaxAge(60 * 60 * 24 * 30);
response.addCookie(cookie);
flag=true;
response.getWriter().write("<h1>欢迎回来,您上次访问时间为"+value+"</h1>");
break;
}
}
}
if (cookies == null || cookies.length==0 || !flag){
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println("编码前:"+time);
//url编码
time = URLEncoder.encode(time, "utf-8");
System.out.println("编码后:"+time);
Cookie lastTime = new Cookie("lastTime", time);
lastTime.setMaxAge(60 * 60 * 24 * 30);
response.addCookie(lastTime);
response.getWriter().write("<h1>欢迎您首次访问</h1>");
}
}
?
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
//url解码 value = URLDecoder.decode(value, "UTF-8");
System.out.println("编码前:"+time);
//url编码 time = URLEncoder.encode(time, "utf-8");
System.out.println("编码后:"+time);
以上是关于会话技术--cookie的主要内容,如果未能解决你的问题,请参考以下文章