Cookie、Session
Cookie、Session
-
会话
-
会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话;
有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学,曾经来过,称之为有状态会话;
-
保存会话的两种技术
Cookie
- 客户端技术(响应、请求)
Session
- 服务器行为,利用该技术可以保存用户的会话信息
例如网站的登陆即使用了该功能,来方便用户的登陆操作。
Cookie
cookie:一般会保存在本地的 用户目录下 appdata;
一个网站cookie是否存在上限!聊聊细节问题
- 一个Cookie只能保存一个信息;
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie;
- Cookie大小有限制4kb;
- 300个cookie浏览器上限
删除Cookie;
- 不设置有效期,关闭浏览器,自动失效;(默认)
- 设置有效期时间为 0 ;设置 Cookie的保存时间为0【cookie.setMaxAge(0)】
注意
- cookie默认有效期是在浏览器关闭后失效,如果设置了有效期就在过期后失效
编写操作cookie的Servlet应用类
@WebServlet("/cook1")
public class CookieTest 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();
//判断Cookie是否存在
if(cookies!=null){
out.write("你上次访问的时间是:");
for (int i=0;i<cookies.length;i++){
Cookie cookie = cookies[i];
//获取cookie的名字
if ("lastLoginTime".equals(cookie.getName())){
long l = Long.parseLong(cookie.getValue());
Date date = new Date(l);
out.write(date.toLocaleString());
}
}
}else {
out.write("创建cookie");
}
//服务器给客户端响应Cookie
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
resp.addCookie(cookie);
}
}
Session(重点)
什么是session?
- 服务器会给每一个用户(浏览器)创建一个session对象
- 一个session独占一个浏览器,主要浏览器没有关闭,这个session就存在
- 用户登录后,整个网站它都可以访问——>保存用户的信息,保存购物车的信息。
Session和Cookie的区别
- Cookie是把用户的数据写给用户的浏览器,浏览器保存
- Session把用户的数据写到用户独占的Session中,在服务器端保存
- Session对象由服务器创建
使用场景:
- 保存一个登录用户的信息;
- 购物车信息
- 在整个网站中经常会使用的数据
在Session中保存一个对象和一个字符串
@WebServlet("/sess01")
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.setAttribute("name", "张三");
//获取session的id
String id = session.getId();
//判断session是否新创建
if (session.isNew()) {
resp.getWriter().write("session创建成功!id=" + id);
} else {
resp.getWriter().write("session已经存在了,id=" + id);
}
resp.getWriter().write(session.getAttribute("name").toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
-
获取session中存储的用户信息
在Session中保存一个对象和一个字符串
@WebServlet("/sess01")
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.setAttribute("name", "张三");
session.setAttribute("person",new Person("李四",20));
//获取session的id
String id = session.getId();
//判断session是否新创建
if (session.isNew()) {
resp.getWriter().write("session创建成功!id=" + id);
} else {
resp.getWriter().write("session已经存在了,id=" + id);
}
resp.getWriter().write(session.getAttribute("name").toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
@WebServlet("/sess02")
public class SessionDemo02 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();
String name = (String) session.getAttribute("name");
Person person = (Person) session.getAttribute("person");
System.out.println(person.toString());
System.out.println(name);
}
}
手动清除session中的参数
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
String name = (String) session.getAttribute("name");
System.out.println(name);
session.removeAttribute("name");
session.invalidate();
}
}
设置自动Session自动保存的最大时长(web.xml)
<session-config>
<!--设置Session10分钟后自动失效-->
<session-timeout>10</session-timeout>
</session-config>

浙公网安备 33010602011771号