前言:
本文内容:Cookie讲解、Session讲解
推荐免费JavaWeb入门到实战视频:【狂神说Java】JavaWeb入门到实战_哔哩哔哩_bilibili
Cookie讲解
会话
用户打开浏览器,点击多个超链接,访问多个web资源,到关闭浏览器的整个过程称为会话;
有状态会话
每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”;一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束。
保存会话的两种技术
Cookie
Session
Cookie
- 从请求中拿到cookie信息
- 服务器响应给客户端的cookie
1 2 3 4 5 6
| Cookie[] cookies = req.getCookies(); cookie.getName(); cookie.getValue(); new Cookie("lastLoginTime",System.currentTimeMillis()+""); cookie.setMaxAge(24*60*60); resp.addCookie(cookie);
|
Cookie小拓展:
- 一个Cookie只能存在一个信息
- 一个站点可以给浏览器发送多个cookie,最多存放20个cookie
- cookie大小限制4kb
- 300个cookie浏览器上限制
删除Cookie
- 不设置有效期,关闭浏览器后自动失效
- 设置有效时间为0
编码解码:
1 2 3
| URLEncoder.encode("张三","utf-8"); URLDecoder.decode(cookie,getValue(),"utf-8")
|
Cookie练习:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.jokerdig.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 CookieDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); Cookie[] cookies = req.getCookies(); if(cookies!=null){ out.write("你上一次访问的时间是:"); for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; if(cookie.getName().equals("lastLoginTime")){ long lastLoginTime = Long.parseLong(cookie.getValue()); Date date = new Date(lastLoginTime); out.write(date.toLocaleString()); } } }else{ out.write("这是第一次访问网站"); } 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); } }
|
Session讲解
Session
服务器为了保存用户状态而创建的一个特殊的对象。(例如登录信息,购物车信息等)
当浏览器第一次访问服务器时,服务器创建一个session对象(该 对象有一个唯一的id,一般称之为sessionId),服务器会将sessionId 以cookie的方式发送给浏览器。
当浏览器再次访问服务器时,会将sessionId发送过来,服务器依据 sessionId就可以找到对应的session对象。
Session和Cookie的区别
- Cookie把用户的数据写给用户的浏览器,浏览器保存(可保存多个)
- Session把用户的数据写到用户独占Session中,服务器端保存(保存重要信息,减少服务器资源浪费)
- Session对象由服务创建
Session使用场景
- 保存用户登录信息
- 购物车信息
- 将整个网站中经常使用的数据放入session
Session练习
发送Session
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.jokerdig.servlet;
import com.jokerdig.pojo.Person;
import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter;
public class SessionDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); HttpSession session = req.getSession();
session.setAttribute("name",new Person("李四",20)); String sessionid = session.getId(); if (session.isNew()){ resp.getWriter().write("session创建成功,sessionID:"+sessionid); }else{ resp.getWriter().write("session已经被创建,sessionID:"+sessionid); }
Cookie cookie = new Cookie("JSESSIONID", sessionid); resp.addCookie(cookie); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
|
接收Session
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.jokerdig.servlet;
import com.jokerdig.pojo.Person;
import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException;
public class SessionDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); HttpSession session = req.getSession();
Person person = (Person) session.getAttribute("name"); System.out.println(person.toString()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
|
手动注销Session
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.jokerdig.servlet;
import com.jokerdig.pojo.Person;
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 SessionDemo3 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); HttpSession session = req.getSession(); session.removeAttribute("name"); session.invalidate(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
|
Web.xml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true">
<servlet> <servlet-name>cookie</servlet-name> <servlet-class>com.jokerdig.servlet.CookieDemo1</servlet-class> </servlet> <servlet-mapping> <servlet-name>cookie</servlet-name> <url-pattern>/cookie</url-pattern> </servlet-mapping>
<servlet> <servlet-name>session</servlet-name> <servlet-class>com.jokerdig.servlet.SessionDemo1</servlet-class> </servlet> <servlet-mapping> <servlet-name>session</servlet-name> <url-pattern>/se</url-pattern> </servlet-mapping> <servlet> <servlet-name>session2</servlet-name> <servlet-class>com.jokerdig.servlet.SessionDemo2</servlet-class> </servlet> <servlet-mapping> <servlet-name>session2</servlet-name> <url-pattern>/se2</url-pattern> </servlet-mapping>
<servlet> <servlet-name>session3</servlet-name> <servlet-class>com.jokerdig.servlet.SessionDemo3</servlet-class> </servlet> <servlet-mapping> <servlet-name>session3</servlet-name> <url-pattern>/se3</url-pattern> </servlet-mapping>
<session-config>
<session-timeout>15</session-timeout> </session-config> </web-app>
|