一、 Cookie定义与操作

1. 1 定义
  1. cookie1是服务器通知客户端保存键值对的一种技术,

    1. 客户端(浏览器端)用于保存数据的一种技术。
  2. 客户端有了cookie后,每次请求都发送给服务器。

  3. 每个cookie大小不超过4kb;
  4. cookie应用场景:

    唯品会电商购物:购物车中的订单信息纯存储到cookie中。

1.2 创建cookie
  1. 创建cookie对象Cookie cookie = new Cookie(key, value)
  2. 通知客户端保存数据:cookie :response.addCookie(cookie)

测试案例:

  • 重写service方法,防止中文乱码;
  • 创建cookie
  • 配置xml文件资源路径。

    1. public class Cookietest extends HttpServlet {
    2. @Override
    3. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    4. req.setCharacterEncoding("utf-8");
    5. resp.setContentType("text/html;charset=utf-8");
    6. creatcookie(req,resp);
    7. }
    8. public void creatcookie(HttpServletRequest req, HttpServletResponse resp){
    9. Cookie cookie = new Cookie("key1","value1");
    10. resp.addCookie(cookie);
    11. try {
    12. resp.getWriter().print("cookie保存成功");
    13. } catch (IOException e) {
    14. e.printStackTrace();
    15. }
    16. }
    17. }

    测试结果
    image.png

  1. cookie每次创建都需要添加保存 resp.addCookie(cookie)
  2. cookie不能单个获取必须一次全部获取

获取cookie Cookie[] cookies = req.getCookies()
随后对其进行遍历操作获取指定cookie
一般也可采用封装一个cookie类。

  1. cookie工具类的封装

1.3 cookie值得修改
  1. 替换值:
  • 直接再创建+保存一个新得cookie相同得key不同得values,用新得来替换旧的。
  • 获取需要修改value得cookie,通过setValue方法来修改value得值

    1. 需要再添加一次cookie来覆盖原来得值。

1.4 cookie的生命周期
  1. 默认时session级别的,浏览器开启到关闭。
  2. cookie得生命控制:
  • setMaxAge(数字)这里数字得单位为秒
  • 正数表示指定秒数后过期;
  • 负数表示过了浏览器这一步cookie就会被删除(默认值为-1)
  • 零,表示马上删除cookie

二、Cookie值渲染到jsp页面/模板引擎上

  1. 首先创建渲染cookie对应得方法:

    1. public void showCookie(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    2. Cookie cookie = new Cookie("username","jack");
    3. resp.addCookie(cookie);
    4. resp.sendRedirect("http://localhost:8080/web/JSP/cookietest.jsp");
    5. }
  2. 对应得jsp文件

    1. <body>
    2. <%--注意如果只获取cookie是获取得全部cookie
    3. 如果获取cookie.username获取的是u而那么这个名字的cookie实例
    4. 如果获取的的是cookie.username.value
    5. 获取的是名为uesername这个cookie对应的value--%>
    6. ${cookie.username.value}
    7. </body>

通过cookie来实现免密登陆

通过修改《学习信息管理系统》的login方法以及login.jsp来实现免密登录。

  1. //创建cookie
  2. Cookie cookie1 = new Cookie("username",username);
  3. //添加cookie
  4. response.addCookie(cookie1);
  5. //创建cookie
  6. Cookie cookie2 = new Cookie("password",password);
  7. //添加cookie
  8. response.addCookie(cookie2);
  9. //重定向跳转页面;
  10. response.sendRedirect("http://localhost:8080/web/student?flag=list");
  1. <td> 登陆用户名:</td>
  2. <td><input type="text" name="username" value="${cookie1.username.value}"></td>
  3. </tr>
  4. <tr>
  5. <td> 登陆密码:</td>
  6. <td><input type="password" name="password" value="${cookie2.password.value}"></td>

登陆后返回登陆页面依旧保留登陆信息,实现免密登陆
image.png

三、session的基本操作

3.1 sesion会话
  1. session是维护客户端和服务端关联的技术
  2. 根据会话id来维护。
  3. 特点:在服务器的任何位置创建session,该对象永远是单例的,指向同一内存地址;

3.2 创建sesion会话
  1. request.getSession() 第一次调用创建Session会话。
  2. isNew();判断是否是刚刚创建出来的。
  3. getId();得到session的会话id。

3.3 sesion生命周期
  1. session默认生命周期30mins,也可以自己设置。

    1. HttpSession session = req.getSession();
    2. session.getMaxInactiveInterval();
    3. session.setMaxInactiveInterval();
  2. session.invalidate()方法:让session立即失效。

3.3 sesion维护关联
  1. session是服务端保存数据的手段,cookie是客户端保存数据的手段

    1. session实例的会话idJSESSTONID)通过响应头的方式给客户端传<br /> 过去。
  2. 通过JSESSTONID可以知道服务的链接关系。

3.4 sesion在案例中的应用

在实际应用中我们需要在登陆时把登陆信息存在session中而不是只在后端做一个校验这样是一个伪登陆;
在做登陆模块时,在用户登陆时需要把用户信息保存在session中

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%--引入jstl标签库--%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  4. <html>
  5. <head>
  6. <title>学生列表</title>
  7. <style>
  8. body{
  9. background-color: darkslategray;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <c:if test="${not empty user}">
  15. <h1 align="center">学生列表页</h1>
  16. <%--不用新封装方法,就直接用getStudentList--%>
  17. <form action="http://localhost:8080/web/student?flag=list" method="post">
  18. 根据姓名模糊查询:<input type="text" name="likename" value="${likename}"> <input type="submit" value="搜索">
  19. </form>
  20. <div style="font-size: 10px;float: right">当前登陆用户为:<span style="color: #0040ff">${user.username}</div>
  21. <table align="center" border="1px">
  22. <tr>
  23. <td colspan="3" align="center"><a href="http://localhost:8080/web/JSP/add.jsp">学生添加</a></td>
  24. <td colspan="3" align="center"><a href="http://localhost:8080/web/student?flag=loginOut">注销</a></td>
  25. </tr>
  26. <tr>
  27. <th>学生姓名</th>
  28. <th>学生性别</th>
  29. <th>学生年纪</th>
  30. <th>学生生日</th>
  31. <th>学生地址</th>
  32. <th>操作</th>
  33. </tr>
  34. <c:forEach items="${students}" var="student">
  35. <tr>
  36. <td>${student.name}</td>
  37. <td>${student.sex == 1? '男' : '女'}</td>
  38. <td>${student.age}</td>
  39. <td>${student.birthday}</td>
  40. <td>${student.address}</td>
  41. <td>
  42. <a href="http://localhost:8080/web/student?flag=deleteStudentById&id=${student.id}">删除</a>
  43. <a href="http://localhost:8080/web/student?flag=showStudentById&id=${student.id}">编辑</a>
  44. </td>
  45. </tr>
  46. </c:forEach>
  47. </table>
  48. </c:if>
  49. <c:if test="${empty user}">
  50. <a href="http://localhost:8080/web/JSP/login.jsp">当前还未登陆请登陆后再访问</a>
  51. </c:if>
  52. </body>
  53. </html>
  1. /*
  2. * 注销当前用户
  3. * 使得session失效即可
  4. * */
  5. private void loginOut(HttpServletRequest request, HttpServletResponse response) {
  6. //获取session对象
  7. HttpSession session = request.getSession();
  8. //使当前session对象失效
  9. session.invalidate();
  10. //重定向到列表页面;
  11. try {
  12. response.sendRedirect("http://localhost:8080/web/student?flag=list");
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }