zoukankan      html  css  js  c++  java
  • java之 Cookie&Session

    0x01、Cookie

    cookie 概念:客户端会话技术,将数据保存到客户端

    常见方法:

    1. 创建Cookie对象,绑定数据
    	new Cookie(String name, String value) 
    2. 发送Cookie对象
    	response.addCookie(Cookie cookie)
    
    3. 获取Cookie,拿到数据
    	Cookie[]  request.getCookies() 
    
    4. setMaxAge(int seconds)
    
    正数:将Cookie数据写到硬盘的文件中。持久化存储。并指定cookie存活时间,时间到后,cookie文件自动失效
    
    负数:默认值
    
    零:删除cookie信息
    		
    5. setPath(String path):设置cookie的获取范围。默认情况下,设置当前的虚拟目录
    * 如果要共享,则可以将path设置为"/",cookie默认情况下不共享	
    
    6. setDomain(String path):如果设置一级域名相同,那么多个服务器之间cookie可以共享
    	
    

    cookieservlet1代码:

    @WebServlet("/CookieServlet")
    public class CookieServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Cookie cookie = new Cookie("name","hello");
            cookie.setMaxAge(10);  //cookie 存储10秒
            cookie.setPath("/");  //设置共享
    
            response.addCookie(cookie);
    
        }
    

    cookieservlet2代码:

    @WebServlet("/CookieServlet2")
    public class CookieServlet2 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println("cookie名"+name);
                System.out.println("cookie值"+value);
            }
        }
    }
    

    0x02、Session

    服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象中

    常用方法:

     request.getSession()     :获取session对象
     
     
     session方法:
     
    Object getAttribute(String name)  
    void setAttribute(String name, Object value)
    void removeAttribute(String name)  
     
    

    Cookie 和Session 不同的地方是 cookie是存在于客户端,而session是存在于服务器上。

    如果客户端关闭后,服务端不关闭,session需要相同,则可以创建Cookie,键为JSESSIONID,设置最大存活时间,让cookie持久化保存。

    @WebServlet("/CookieServlet")
    public class CookieServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            HttpSession session = request.getSession();  //获取session对象
            Cookie cookie = new Cookie("JSESSIONID",session.getId());  //cookie传入session的值
    
    
            cookie.setMaxAge(10);  //cookie 存储10秒
            cookie.setPath("/");  //设置共享
    
            response.addCookie(cookie);
    
        }
    }
    

    由此可见,session是依赖于cookie的。

    当服务器关闭后,会将session序列化到硬盘里面,重启的时候seesion文件会转换为内存中的session对象。

    session默认的过期时间是30秒,如果需要设置可以到tomcat/conf/web.xml文件里面修改。

    <session-config>
    		        <session-timeout>60</session-timeout>
    		    </session-config>
    
  • 相关阅读:
    25.C++- 泛型编程之函数模板(详解)
    Windows10 + Visual Studio 2017 + CMake +OpenCV编译、开发环境配置及测试
    终于解决了python 3.x import cv2 “ImportError: DLL load failed: 找不到指定的模块” 及“pycharm关于cv2没有代码提示”的问题
    Python的开源人脸识别库:离线识别率高达99.38%(附源码)
    python获取公网ip的几种方式
    Chrome与chromedriver.exe的版本对应
    Google Gson用法
    idea 报错javax/xml/bind/DatatypeConverter
    org.slf4j:slf4j-api:添加日志管理
    基本使用——OkHttp3详细使用教程
  • 原文地址:https://www.cnblogs.com/0x7e/p/14331491.html
Copyright © 2011-2022 走看看