参考狂神说
Cookie
1.会话:
用户打开一个浏览器,
点击了很多超链接,访问了多个web资源,
关闭浏览器,这个过程称为一个会话
2.有状态会话:一个同学来过教室,下
保存会话两个技术:
1. Cookie 客户端技术(响应,请求)
1. 从请求中拿到cookie信息
Cookie[] cookies = req.getCookies();
获取cookie中的key和value
cookie.getValue();
cookie.getName();
2. 服务器响应给客户端cookie
Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
cookie.setMaxAge(24*60*60);//设置cookie的有效期
resp.addCookie(cookie);
Cookie类
从Cookie类的构造函数可知,Cookie中只能存放两个字符串,name和value
第一次访问:
第二次访问
respond中是此次访问中服务器返回的cookie
request中是request携带的 在上次访问中server响应的cookie
关闭浏览器,删除了request的cookie
可以设置cookie的有效期 eg.
cookie.setMaxAge(24*60*60);
则cookie的有效期是一天,关闭浏览器cookie也不会失效。
删除cookie:
1. 不设置有效期,关闭浏览器,自动失效
2. 设置有效期为0
eg
http://localhost:8080/javaweb_session_cookie_war/c1
package com.kuang.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
public class CookieDemo01 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//服务器,告诉你,你来的时间,把这个时间封装成为一个信件,你下次带来,我就知道你来了
//解决中文乱码
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
//cookie,服务器从客户端获取
Cookie[] cookies= req.getCookies();
//判断cookie是否存在
if(cookies != null){
//如果存在怎么办
out.write("你上一次访问的时间是:");
for(int i = 0; i < cookies.length; i++){
Cookie cookie = cookies[i];
//获取cookie的名字
if(cookie.getName().equals("lastLoginTime")){
//获取cookie中的值
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
}else{
out.write("这是您第一次访问本站");
}
//服务器给客户端响应一个cookie
Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
cookie.setMaxAge(24*60*60);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
http://localhost:8080/javaweb_session_cookie_war/c1
package com.kuang.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
public class CookieDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
cookie.setMaxAge(0);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
首先访问/c1,则有了一个cookie
在访问/c2,则cookie失效
Session
什么是session:
1. 服务器会给每个用户(浏览器)创建一个session对象
2. 一个session独占一个浏览器,只要浏览器没有关闭,这个session就存在
3. 用户登录后,整个网站他都可以访问 --- 保存用户信息;保存购物车的信息
打开浏览器,就会创建一个新的session
客户端(浏览器端):
我电脑上有两个浏览器,打开浏览器1,服务器为浏览器1创建一个session,只要改session没过期,这个浏览器就一直拥有这个session
http://localhost:8080/javaweb_session_cookie_war/s1
package com.kuang.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text.html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
//给session中存东西
session.setAttribute("name","qinjiang");
//获取session的ID
String sessionId = session.getId();
//判断session是不是新创建的
if(session.isNew()){
resp.getWriter().write("session is successfully created ID: " + sessionId);
}else{
resp.getWriter().write("session is already existed in the server ID: " + sessionId);
}
//session创建的时候做了什么事情
// Cookie cookie = new Cookie("JSESSIONID",sessionId);
//resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
第一次访问/s1
第二次访问/s1
http://localhost:8080/javaweb_session_cookie_war/s3
package com.kuang.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("name");
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
访问/s3后,当前session失效,会创建新的session