Cookie:

cookie是一种客户端的状态管理技术。

当浏览器向服务器发送请求的时候,服务器会将少量的数据以set-cookie消息头的方式发送给浏览器,当浏览器再次访问服务器时,会将这些数据以cookie消息头的方式发送给服务器.

Cookie是由HTTP服务器设置的,保存在浏览器中,但HTTP协议是一种无状态协议,在数据交换完毕后,服务器端和客户端的链接就会关闭,每次交换数据都需要建立新的链接。

实际上就是在客户端与服务端交换的一小段数据(一个name/string对)。

示例图:

Cookie中的常用方法:

Cookie cookie=new Cookie(String name,String value) 构造一个cookie对象

response.addCookie(Cookie cookie) 是将一个cookie对象传入客户端。

request.getCookies() 得到所有的cookie对象

cookie.getName() 得到此cookie对象的名字

cookie.getValue() 得到对应名称的cookie的值

cookie.setMaxAge() 设置过期时间

Cookie技术的在web应用程序中的一般实现:

实例代码:

  1. package gac.xdp.cookie;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.Date;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.Cookie;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. /**
  13. * @author gacl
  14. * cookie实例:获取用户上一次访问的时间
  15. */
  16. public class CookieDemo01 extends HttpServlet {
  17.  
  18. public void doGet(HttpServletRequest request, HttpServletResponse response)
  19. throws ServletException, IOException {
  20. //设置服务器端以UTF-8编码进行输出
  21. response.setCharacterEncoding("UTF-8");
  22. //设置浏览器以UTF-8编码进行接收,解决中文乱码问题
  23. response.setContentType("text/html;charset=UTF-8");
  24. PrintWriter out = response.getWriter();
  25. //获取浏览器访问访问服务器时传递过来的cookie数组
  26. Cookie[] cookies = request.getCookies();
  27. //如果用户是第一次访问,那么得到的cookies将是null
  28. if (cookies!=null) {
  29. out.write("您上次访问的时间是:");
  30. for (int i = 0; i < cookies.length; i++) {
  31. Cookie cookie = cookies[i];
  32. if (cookie.getName().equals("lastAccessTime")) {
  33. Long lastAccessTime =Long.parseLong(cookie.getValue());
  34. Date date = new Date(lastAccessTime);
  35. out.write(date.toLocaleString());
  36. }
  37. }
  38. }else {
  39. out.write("这是您第一次访问本站!");
  40. }
  41.  
  42. //用户访问过之后重新设置用户的访问时间,存储到cookie中,然后发送到客户端浏览器
  43. Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+"");//创建一个cookie,cookie的名字是lastAccessTime
  44. //将cookie对象添加到response对象中,这样服务器在输出response对象中的内容时就会把cookie也输出到客户端浏览器
  45. response.addCookie(cookie);
  46. }
  47.  
  48. public void doPost(HttpServletRequest request, HttpServletResponse response)
  49. throws ServletException, IOException {
  50. doGet(request, response);
  51. }
  52.  
  53. }

Session:

session是一种服务器端的状态管理技术。

session是基于cookie的技术。当浏览器访问服务器时,服务器会创建一个session对象(该对象有一个唯一的id号,称之为sessionId)服务器在默认的情况下,会将sessionId以cookie的方式,发送给浏览器,浏览器会将sessionId保存到内存中。当浏览器再次访问服务器时,会将sessionId发送给服务器,服务器依据sessionId就可以找到之间创建的session对象。

示例图:

Session的简单案例:

  1. @WebServlet("/SeesionServletDemo1")
  2. public class SeesionServletDemo1 extends HttpServlet {
  3. private static final long serialVersionUID = 1L;
  4.  
  5. /**
  6. * @see HttpServlet#HttpServlet()
  7. */
  8. public SeesionServletDemo1() {
  9. super();
  10. // TODO Auto-generated constructor stub
  11. }
  12.  
  13. /**
  14. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  15. * response)
  16. */
  17. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  18. throws ServletException, IOException {
  19.  
  20. // 创建session对象
  21. HttpSession session = request.getSession();
  22. System.out.println(session.getId());
  23.  
  24. session.setAttribute("name", "zhangsan");
  25.  
  26. session.setAttribute("student", new Student(1, "zhangsan", 12));
  27. Student stu = (Student) session.getAttribute("student");
  28.  
  29. stu.setName("lisi");
  30.  
  31. // 设置过期时间 单位是秒
  32. // session.setMaxInactiveInterval(2);
  33. // 直接销毁session 注销登录
  34. // session.invalidate();
  35.  
  36. }
  37.  
  38. /**
  39. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  40. * response)
  41. */
  42. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  43. throws ServletException, IOException {
  44. // TODO Auto-generated method stub
  45. doGet(request, response);
  46. }
  47.  
  48. }
  1. public class Student {
  2.  
  3. private Integer id;
  4. private String name;
  5. private Integer age;
  6. public Integer getId() {
  7. return id;
  8. }
  9. public void setId(Integer id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public Integer getAge() {
  19. return age;
  20. }
  21. public void setAge(Integer age) {
  22. this.age = age;
  23. }
  24. public Student(Integer id, String name, Integer age) {
  25. super();
  26. this.id = id;
  27. this.name = name;
  28. this.age = age;
  29. }
  30. public Student() {
  31. super();
  32. // TODO Auto-generated constructor stub
  33. }
  34. }
  1. @WebServlet("/SeesionServletDemo2")
  2. public class SeesionServletDemo2 extends HttpServlet {
  3. private static final long serialVersionUID = 1L;
  4.  
  5. /**
  6. * @see HttpServlet#HttpServlet()
  7. */
  8. public SeesionServletDemo2() {
  9. super();
  10. // TODO Auto-generated constructor stub
  11. }
  12.  
  13. /**
  14. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  15. * response)
  16. */
  17. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  18. throws ServletException, IOException {
  19.  
  20. // 创建session对象
  21. HttpSession session = request.getSession();
  22.  
  23. System.out.println(session.getAttribute("name"));
  24.  
  25. Student stu = (Student) session.getAttribute("student");
  26. System.out.println(stu.getName());
  27.  
  28. }
  29.  
  30. /**
  31. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  32. * response)
  33. */
  34. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  35. throws ServletException, IOException {
  36. // TODO Auto-generated method stub
  37. doGet(request, response);
  38. }
  39.  
  40. }

Cookie和Session 的一般差别与联系:

1,session 在服务器端,cookie 在客户端(浏览器)

2,session 默认被存在在服务器的一个文件里(不是内存)

3,session 的运行依赖 session id,而 session id 是存在 cookie 中的,也就是说,如果浏览器禁用了 cookie ,同时 session 也会失效(但是可以通过其它方式实现,比如在 url 中传递 session_id)

4,session 可以放在 文件、数据库、或内存中都可以。

5,用户验证这种场合一般会用 session

Session是在服务端保存的一个数据结构,用来跟踪用户的状态,这个数据可以保存在集群、数据库、文件中。


Cookie是客户端保存用户信息的一种机制,用来记录用户的一些信息,也是实现Session的一种方式。

Cookie和Session的介绍与认识的更多相关文章

  1. Cookie和Session 简单介绍

    cookie :     1.cookie是存在客户端(浏览器)的进程内存中和客户端所在的机器硬盘上     2.cookie只能能够存储少量文本,大概4K大小     3.cookie是不能在不同浏 ...

  2. cookie和session的介绍

    1.cookie和session cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此产生cookie. cookie的工作原理是:由服务器产生 ...

  3. cookie跟session自我介绍

    Cookie是什么? cookie说的直白点就是保存在用户浏览器端的一个键值对,举个例子,你现在登录了京东商城,你把浏览器关闭之后,你再打开京东,你还是可以对你的账户继续操作,已经购买的商品,订单都是 ...

  4. Cookie和Session的总结

    1.开篇 在之前学习这一段的时候我一直有点没弄清楚,其实对Session这块的理解还可以,但是Cookie感觉始终还是欠缺点火候.之后的很长一段时间都基本上很少用Cookie了,渐渐的也淡忘了这一块的 ...

  5. 前端Cookie与Session的区别

    我们在实际生活中总会遇到这样的事情,我们一旦登录(首次输入用户名和密码)某个网站之后,当我们再次访问的时候(只要不关闭浏览器),无需再次登录.而当我们在这个网站浏览一段时间后,它会产生我们浏览的记录, ...

  6. 前端页面——Cookie与Session有什么区别

    我们在实际生活中总会遇到这样的事情,我们一旦登录(首次输入用户名和密码)某个网站之后,当我们再次访问的时候(只要不关闭浏览器),无需再次登录.而当我们在这个网站浏览一段时间后,它会产生我们浏览的记录, ...

  7. 【10】Cookie和Session

    一.cookie和session的介绍 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要"保持状态",因此cookie就是在这样一个场景下 ...

  8. Django(十)COOKIE和session

    https://www.cnblogs.com/haiyan123/p/7763169.html from django.shortcuts import render,redirect # Crea ...

  9. Python框架----cookie和session

    一.cookie和session的介绍 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie ...

随机推荐

  1. erlang学习笔记

    安装 Ubuntu Server上: sudo apt-get install erlang 如果安装时下载 太慢,可手工下载deb包( esl-erlang_16.a-rc1_ubuntu_prec ...

  2. Rip CD on Ubuntu

    用Mint自带的banshee可以把CD转换为ogg文件,[Media -> Import Media],然后选择Audio CD,但只能转换为ogg格式,好像不能自动获取ID3标签:比较好的方 ...

  3. kubernetes中headless类型的service

    目录 初识headless类型的service 开始研究headless类型的service headless类型的service之我的理解 初识headless类型的service 第一次使用ran ...

  4. @ConfigurationProperties实现自定义配置绑定

    @ConfigurationProperties使用 创建一个类,类名上方注解,配置prefix属性,如下代码: @ConfigurationProperties( prefix = "he ...

  5. 题解 Six(留坑)

    传送门 考场上搞了个三进制状压,结果正确性假了-- 有想到从约数下手,但觉得就光预处理约数复杂度就爆炸就没往这边想-- 首先是关于约数个数的证明,再一次感谢战神: 因为 \(n = \prod p_i ...

  6. Math.round() 函数返回一个数字四舍五入后最接近的整数。

    语法: Math.round(x); 参数:x 返回值:给定数字的值四舍五入到最接近的整数 描述: 如果参数的小数部分大于 0.5,则舍入到相邻的绝对值更大的整数. 如果参数的小数部分小于 0.5,则 ...

  7. C++ 保存读取二进制

    一.保存二进制 #include <iostream> #include <fstream> int main(){ float* output = new float[100 ...

  8. 初始C3P0连接池

    C3P0连接池只需要一个jar包: 其中我们可以看到有三个jar包: 属于C3P0的jar包只有一个,另外两个是测试时使用的JDBC驱动:一个是mysql的,一个是oracle的: 可以看到在src下 ...

  9. 程序挂了之后别再跟我说让我帮你重启啦! 让supervisor帮你搞定...

    目录 有啥用? 安装 生成配置文件 启动supervisor 自定义配置文件 控制命令 求关注啦 有啥用? 很多我们项目排期进入联调.测试阶段,如果QA同学是直接跟你要一个后端环境的话,那简单点大概率 ...

  10. VS Code 1.60 发布!竟然可以自动检测编程语言了!

    北京时间 2021 年 9 月 3 日凌晨,微软正式发布 2021 年 8 月版的 Visual Studio Code.希望您会喜欢此版本中的许多更新与改进,以下是其中的一些亮点: * 自动语言检测 ...