一.会话技术概述
在日常生活中,A和B之间在打电话过程中一连串的你问我答就是一个会话
在BS模型中,会话可以理解为通过浏览器访问服务端的资源,点击超链接可以进行资源的跳转,直到浏览器关闭过程叫做会话
我们使用会话技术可以解决的是整个会话过程中(通过浏览器浏览服务端资源过程中)会产生数据保存问题
Request域,ServletContext域在保存会话过程中的数据会导致数据访问的一些问题.(每发送一个请求和响应,就会新建一个request和response对象,很难访问)
二.Cookie
2.1 Cookie概述
Cookie是一种在客户端(浏览器)存储用户会话信息的技术
2.2 Cookie创建和获取
/*** 服务端创建Cookie并发送给浏览器* HttpServletResponse方法:* public void addCookie(Cookie cookie)* 将指定 cookie 添加到响应。可多次调用此方法设置一个以上的 cookie。* Cookie类的方法:* 构造方法:* public Cookie(String name, String value)* 构造带指定名称和值的 cookie。*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.addCookie(new Cookie("username","zs"));response.addCookie(new Cookie("username","ls"));//新值覆盖老值response.addCookie(new Cookie("password","123"));}
/*** 获取Cookie中的数据* HttpServletRequest中的方法:* public Cookie[] getCookies()* 获取浏览器发送过来的所有Cookie对象,如果没有发送任何 cookie,则此方法返回 null* Cookie中的方法:* String getName()* 获取当前Cookie对象中封装的name* String getValue()* 获取当前Cookie对象中封装的value*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//请求中会携带着Cookie的数据,所以getCookies()方法在request对象中Cookie[] cookies = request.getCookies();if (cookies!=null) {for (Cookie cookie : cookies) {System.out.println(cookie.getName() + "=" + cookie.getValue());}}else {throw new NullPointerException();}}
2.3 Cookie原理
2.4 Cookie路径
1.Cookie默认设置路径
Cookie会将路径默认设置为请求的服务端路径最后一级路径前面的所有路径
例如:
请求 /day12_CookieAndSession/sendCookie,Cookie的默认路径就是 /day12_CookieAndSession
请求 /day12_CookieAndSession/path01/setCookie,Cookie的默认路径是 /day12_CookieAndSession/path01
2.Cookie不同路径是否携带问题
Cookie的路径:/day12_CookieAndSession/path01
/day12_CookieAndSession/path01/getCookie01 会携带
/day12_CookieAndSession/path01/path02/getCookie02 会携带
/day12_CookieAndSession/getCookie03 不携带
Cookie会在请求跟Cookie相同的路径的Servlet或者该路径下的子级路径的Servlet都会携带 Cookie
如果这个路径既不是Cookie的路径也不是子级路径,那么就不携带这个Cookie
3.设置Cookie的路径
/**
* 手动设置Cookie路径
* public String getPath()
* 手动设置Cookie的路径
*/
/*** 手动设置Cookie路径* public String getPath()* 手动设置Cookie的路径*/
@WebServlet("/path01/setCookie")
public class SetCookie extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Cookie cookie = new Cookie("gender", "male");cookie.setPath(request.getContextPath());//同一级的Servlet以及子级的Servlet在发出请求时都会携带cookieresponse.addCookie(cookie);}
}
/*** 处理Cookie对象的工具类*/
public class CookieUtils {/**** @param name 要查找的Cookie的name* @param cookies 存储了浏览器携带的所有cookie对象* @return 若找到与name匹配的cookie对象则返回,反之则返回null*/public static Cookie getCookie(String name, Cookie[] cookies) {if (cookies != null) {for (Cookie cookie : cookies) {if (cookie.getName().equals(name)) {return cookie;}}}return null; //在cookie数组中没找到所需要的cookie对象或者cookies为null,返回null}
}
2.5 Cookie生命周期
2.5.1 概述
1.Cookie的默认最大存活时间在浏览器会话结束前,一旦浏览器会话结束(浏览器关闭),浏览器会自 动删除Cookie
2.手动设置Cookie最大存活时间
public void setMaxAge(int expiry)
设置 cookie 的最大生存时间,以秒为单位。
2.5.2 设置cookie生命周期
@WebServlet("/setCookieMaxAge")
public class SetCookieMaxAge extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Cookie cookie = new Cookie("id", "789");cookie.setPath(request.getContextPath());cookie.setMaxAge(30);//Cookie的最大存活时间30sresponse.addCookie(cookie);}
}
2.6 手动删除cookie
/*** public void setMaxAge(int expiry)* 将cookie最大存活时间设置成0代表立马删除这个cookie*/@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Cookie[] cookies = request.getCookies();for (Cookie cookie : cookies) {cookie.setMaxAge(0);response.addCookie(cookie);System.out.println("delete success");}/*** 1.服务器端检索到请求中的Cookie。* 2.服务器端调用setMaxAge(0)方法,将Cookie的生命周期设置为立即过期。* 3.服务器端将修改后的Cookie通过response.addCookie(cookie)发送到客户端。* 4.客户端浏览器接收到响应,根据Set-Cookie头中的指令删除该Cookie。*/}
2.7 cookie案例
//获取上一次访问该网页的时间 @Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");Cookie cookie = CookieUtils.getCookie("date", request.getCookies());if (cookie == null) {//如果cookie为空,说明为第一次访问response.getWriter().write("这是您第一次访问");} else {String oldDate = cookie.getValue();//cookie不为空时,访问cookie的value值,由于传递过来的是UTF-8格式的数据,所以需要进行解码String newDate = URLDecoder.decode(oldDate, "UTF-8");response.getWriter().write("您上次的访问时间为" + newDate);}//无论if还是else,最终都要对日期进行更新String date = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());//因为date中包含空格等特殊字符,所以先将date编码为UTF-8格式传递给浏览器String newDate = URLEncoder.encode(date, "UTF-8");response.addCookie(new Cookie("date", newDate));}
}