简介:javaweb操作浏览器器cookie
1.创建并发送 Cookie
package com.gaze;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/setCookie")
public class SetCookieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 创建 Cookie
Cookie cookie = new Cookie("username", "JohnDoe");
// 设置有效期(单位:秒,3600秒为1小时)
cookie.setMaxAge(3600);
// 设置作用路径
cookie.setPath("/");
// 将 Cookie 发送给客户端
response.addCookie(cookie);
response.getWriter().println("Cookie 'username' has been set.");
}
}
cookie.setPath("/")详解:
cookie
是一个javax.servlet.http.Cookie
类的实例,它代表了一个HTTP Cookie,通常用于在客户端(用户的浏览器)和服务器之间传递信息。
setPath(String path)
是Cookie
类的一个方法,用于指定Cookie的有效路径。这个路径决定了浏览器在哪些URL请求中会包含这个Cookie。当你调用
cookie.setPath("/");
时,你实际上是在告诉浏览器,这个Cookie对于当前Web应用程序的根目录及其所有子目录下的所有页面都是有效的。换句话说,只要请求的URL是以域名开头例如:
如果你设置
cookie.setPath("/");
,那么无论用户访问http://www.example.com/index.html
http://www.example.com/products/list.html
是http://www.example.com/about/us.html
,浏览器都会发送这个Cookie。如果你设置
cookie.setPath("/products");
,那么这个Cookie只会在用户访问以/products
开头的页面时发送,如http://www.example.com/products/list.html
,但不会在访问http://www.example.com/about/us.html
时发送。
Cookie 的设置、读取、修改和删除:
-
package com.gaze; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; @WebServlet("/setCookie") public class SetCookieServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 创建 Cookie Cookie cookie = new Cookie("username", "JohnDoe"); // 设置有效期(单位:秒,3600秒为1小时) cookie.setMaxAge(3600); // 设置作用路径 cookie.setPath("/"); // 将 Cookie 发送给客户端 response.addCookie(cookie); response.getWriter().println("Cookie 'username' has been set."); } }
package com.gaze; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/getCookie") public class GetCookieServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException { Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { // 输出 Cookie 的名称和值 response.getWriter().println(cookie.getName() + ": " + cookie.getValue()); } } else { response.getWriter().println("No cookies found."); } } }
package com.gaze; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/deleteCookie") public class DeleteCookieServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie cookieToDelete = new Cookie("username", null); cookieToDelete.setMaxAge(0); // 设置有效期为 0 cookieToDelete.setPath("/"); response.addCookie(cookieToDelete); response.getWriter().println("Cookie has been deleted."); } }
package com.gaze; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/updateCookie") public class UpdateCookieServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException { Cookie updatedCookie = new Cookie("username", "JaneDoe"); updatedCookie.setMaxAge(3600); // 更新有效期 updatedCookie.setPath("/"); response.addCookie(updatedCookie); response.getWriter().println("Cookie has been updated."); } }
注意:js获取cookie,可以获取token,但是获取不不到JSESSIONID,因为 http-only原因