本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名原文链接~~~

JavaWeb(8)之Cookie和Session

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:


Cookie

Cookie 实际上就是存储在浏览器端的一个小数据。因为 http 请求是无状态的,即客户端和服务器通讯的时候是无状态的。Cookie 的作用就是可以帮我们在每次请求的时候携带数据到服务端,而服务端可以根据接收到的携带的数据间接实现状态持久。

看如下示例:

// /cookie/set
package com.zze.servlet;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SetCookieServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Cookie cookie1 = new Cookie("name", "zhangsan");
        cookie1.setDomain("localhost"); // 设置请求指定域时才携带 cookie
        cookie1.setPath("/cookie"); // 设置请求指定路径时才携带 cookie
        cookie1.setMaxAge(60); // 设置有效时间,单位为“秒”
        Cookie cookie2 = new Cookie("age","20");
        response.addCookie(cookie1);
        response.addCookie(cookie2);
        System.out.println("请求成功");
    }
}
// /cookie/get
package com.zze.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetCookieServlet extends HttpServlet {
    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(String.format("name:%s,value:%s",name,value));
        }
    }
}

先访问 localhost:8080/cookie/set
image.png
再访问 localhost:8080/cookie/get,控制台输出:

name:age,value:20
name:name,value:zhangsan
name:_gcl_au,value:1.1.931601231.1544668650
name:_ga,value:GA1.1.337106894.1544668656
name:_pk_id.5.1fff,value:2b0610186745f3b2.1545900897.3.1545987031.1545986337.
name:_gid,value:GA1.1.1827212045.1546481630

image.png

Session

Session 是基于 Cookie 实现的,它实际上就是通过 Cookie 携带唯一 SessionId 值帮助客户端在服务端维护一块对应的独立的内存空间。

看如下示例:

// /session/set
package com.zze.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SetSessionServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.setAttribute("name", "zhangsan");
        session.setAttribute("age",21);
        System.out.println("设置 Session 成功");
    }
}
// /session/get
package com.zze.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;

public class GetSessionServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Enumeration<String> attributeNames = session.getAttributeNames();
        while (attributeNames.hasMoreElements()){
            String name = attributeNames.nextElement();
            Object value = session.getAttribute(name);
            System.out.println(String.format("name:%s value:%s",name,value.toString()));
        }
    }
}

先访问 localhost:8080/session/set,再访问 localhost:8080/session/get,控制台输出:

设置 Session 成功
SessionId:A2582CD80E2AFCE986D15C9C4C5FCF68
name:name value:zhangsan
name:age value:21

image.png
可以看到,请求 localhost:8080/session/get 时携带的名为 JSESSIONID 的 Cookie 值就是输出的 SessionId

# JavaWeb  

如果这篇文章对您有帮助,可点击下方链接分享给你的朋友们😋,如果遇到问题欢迎评论、留言~~~😇

评论

公众号:zze_coding

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×