Cookie and Session
会话
会话:用户打开一个浏览器,点击了很多超链接,访问多个Web资源,关闭浏览器,这个过程称之为会话。
有状态会话:一个服务端证明客户端来过?
- 服务端给客户端一个信件,客户端下次访问服务端时带上信件就可以了;cookie
- 服务器登记你来过了,下次你来的时候我来匹配你。session
保存会话的两种技术
- cookie
- 客户端技术(相应,请求)
- session
- 服务器技术,利用这个技术,可以保存用户的会话信息。我们把信息和数据放在Session中。
常见场景:登录网站之后,无需重复登录。
Cookie
- 从请求中拿到cookie信息
- 服务器相应给客户端cookie
package com.guhuo.servlet;
import javax.servlet.ServletException;
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.io.PrintWriter;
import java.util.Date;
public class CookieDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 服务器,告诉你,你来的时间,把这个时间封装成为一个信件,你下次带来,我就知道你来的时间
// 解决中文乱码
req.setCharacterEncoding("utf-16");
resp.setCharacterEncoding("utf-16");
PrintWriter out = resp.getWriter();
// Cookie,从服务器端从客户端获取
Cookie[] cookies = req.getCookies(); //这里返回一个数组,说明可能会有多个Cookies
// 判断Cookie是否存在
if (cookies != null) {
// 如果存在?
out.write("你上一次访问的时间是:");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
// 获取cookie的名字
String name = cookie.getName();
if(name.equals("lastLoginTime")){
//获取cookie的值
String value = cookie.getValue();
long lastLoginTime = Long.parseLong(value);
Date data = new Date(lastLoginTime);
out.write(data.toLocaleString());
}
}
} else {
out.write("这是你第一次访问本网站"); // 第一次访问该网站时,没有Cookie
}
// 服务给客户端相应一个cookie:
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
cookie.setMaxAge(24*60*60); // 有效期为1天
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
cookie:一般会保存在本地的用户目录的appdata下。
- 一个cookie只能保存一个信息
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
- Cookie大小有限制4kb
- 300个cookie浏览器上限
删除cookie:
- 不设置有效期,关闭浏览器,自动失效;
- 设置有效期时间为0;
Session
什么是Session:
- 服务器会给每个用户(浏览器)创建一个Session对象
- 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在
- 用户登录之后,整个网站它都可以访问
Session和Cookie之间的差别
- Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
- Session把用户的数据写到用户独占Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
- Session对象由服务器创建
使用场景:
- 保存一个登录用户的信息;
- 购物车信息;
- 在整个网站中经常会使用的数据,我们将它保存在Session中;
Session的使用
package com.guhuo.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo 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", "guhuo");
session.setAttribute("name", new Preson("guhuo", 1));
// 获取Session的ID
String sessionId = session.getId();
// 判断Session是不是创建的
if(session.isNew()) {
resp.getWriter().write("session创建成功,ID:" + sessionId);
} else {
resp.getWriter().write("session以及存在与服务器中,ID:" + sessionId);
}
// 注销session
session.removeAttribute("name");
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
会话自动过期,可在web.xml中设置: