0


JavaWeb —— 会话技术(Cookie,Session)

前言 :

什么是会话?

会话是指浏览器访问服务器,访问结束关闭浏览器

为什么需要会话技术?

http是无状态的协议,客户每次读取web页面时,服务器都打开新的会话,服务器不能确认多次请求是否来自与同一个客服端,如果说我们要实现购物车添加或删除商品的功能的话,我们就需要使用到Cookie和Session技术。Cookie和Session是域对象。

具体实现:

例如我们在淘宝页面登录我们自己的账号,下次登录时我们就不需要再次输入账号和密码,直接就可以登录了。这是怎么做到的呢具体实现是使用到了会话技术(Cookie,Session),第一次输入账号密码登录时,我们会将数据提交到服务器,服务器会生成一个CookieId(Cookie数据),返回给客户端,下次登录时,客户端会将CookieId(Cookie数据)携带给浏览器,服务器会根据CookieId来识别是否是同一个客户端。

Cookie(一次会话中多次请求间数据共享问题)

作为后端程序员我们只需要学会Cookie的基本使用(获取Cookie和发送Cookie)

1 发送 Cookie

  1. Cookie cookies = new Cookie("key","value");//获取Cookie对象
  2. response.addCookie(cookies);//发送Cookie

2 获取Cookie对象

  1. request.getCookie();//获取Cookie数组
  2. for (Cookie cookie : cookies)//遍历Cookie
  3. if("username".equals(name)) {//取出自己想要的Cookie
  4. String value = Cookie.getValue();
  5. System.out.println(name + value);
  6. }

Cookie细节:

细节 :

设置Cookie的存活时间(在发送对象前)

  1. 整数表示将Cookie写入浏览器所在本机硬盘中,持久化存储
  2. 负数表示关闭浏览器,Cookie对象将会被销毁
  3. 0表示将删除Cookie对象
  1. cookies.setMAXAge(min)

Cookie存储中文(Cookie不支持中文,要进行编码解码)

在获取Cookie数据前编码

  1. package cookie;/**
  2. * @author 三月水
  3. * version 0.5
  4. * 演示客服端会话技术
  5. */
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8. import javax.servlet.annotation.*;
  9. import java.io.IOException;
  10. import java.net.URLEncoder;
  11. @WebServlet("/aservlet")
  12. public class Aservlet extends HttpServlet {
  13. @Override
  14. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. //获取Cookie对象
  16. String value = "张三";
  17. value = URLEncoder.encode(value,"utf-8");
  18. System.out.println("存储数据"+value);
  19. Cookie cookie = new Cookie("username",value);
  20. //发送Cookie
  21. response.addCookie(cookie);
  22. }
  23. @Override
  24. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. this.doGet(request, response);
  26. }
  27. }

在获取Cookie数据后解码

  1. package cookie;/**
  2. * @author 三月水
  3. * version 0.5
  4. */
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import javax.servlet.annotation.*;
  8. import java.io.IOException;
  9. import java.net.URLDecoder;
  10. @WebServlet("/Bservlet")
  11. public class Bservlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. // 获取cookie
  15. Cookie[] cookies = request.getCookies();
  16. for (Cookie cookie : cookies) {
  17. String name = cookie.getName();
  18. if ("username".equals(name)) {
  19. String value = cookie.getValue();
  20. //获取数据之后解码
  21. value = URLDecoder.decode(value, "utf-8");
  22. System.out.println(name + ":" + value);
  23. }
  24. break;
  25. }
  26. }
  27. @Override
  28. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
  29. ServletException, IOException {
  30. this.doGet(request, response);
  31. }
  32. }

** 核心编解码方法**

  1. URLEncoder.encoder(value,"字符集");//编码
  2. URLDecoder.decoder(value,"字符集");//解码

Session

Session是基于Cookie的,Session实现了HttpSession。

基本使用:

  1. @WebServlet("/demo1")
  2. HttpSession session = request.getSession();//获取Session对象
  3. seession.setAttribute("key","value");
  4. @WebServlet("/demo2")
  5. HttpSession session = request.getSession();//获取Session对象 这里Session对象是同一个
  6. seession.getAttribute("key");

怎么知道他们的Sesion对象是同一个呢?(可以输出session引用,对比地址值)

Session细节:

1.服务器关闭了,Session会被关闭吗? (不会)

钝化 :服务器正常关闭后,Tomcat会自动将Session数据写入硬盘文件中

活化 :再次启动服务器后,会从文件中加载数据到Session中

Session销毁

默认销毁时间为30分钟,可以再xml配置中,修改销毁时间一般修改为100分钟

1 web.xml文件中添加配置
<Session config>
<Session timeout>100</Session timeout>
</Session config>

2 Session.invalidate();//销毁

Cookie 和 Session区别

安全性 : Cookie 不安全(存储客户端,经常通过HTTP协议传输数据到服务器)

Session 安全 (存储在服务器)

数据大小 :

Cookie 大小限制3kb

Session 大小无限制

存活时间:

Cookie 长期存活(几个月至1年)

Session 存活(默认30分钟)

服务器性能:

Cookie不占用服务器资源

Session占用服务器资源

标签: 服务器 前端 运维

本文转载自: https://blog.csdn.net/m0_63573177/article/details/124646584
版权归原作者 三月水_ 所有, 如有侵权,请联系我们删除。

“JavaWeb &mdash;&mdash; 会话技术(Cookie,Session)”的评论:

还没有评论