Cookie attribute

Now that you know these properties, you know how to operate on cookies.

name

Name of Cookie.

value

The value of the Cookie.

maxAge

The Cookie expiration time has the following values. The default value is -1

value instructions
A negative number Cookies become invalid after the browser is closed
0 Clear cookies now
A positive number Set the expiration time, in seconds

path

Cookie valid path, and/indicates that the path is accessible to all projects. If no path is set, only the Cookie path and its subpaths are accessible.

Get all cookies

public static Cookie[] getCookies(HttpServletRequest request) {
    return request.getCookies();
}
Copy the code

Getting a cookie is easy, just from the request.

Gets the specified Cookie by name

public static Cookie getCookieByName(HttpServletRequest request, String name) { if (StringUtils.isBlank(name)) { return null; } Cookie[] cookies = getCookies(request); if (null ! = cookies) { for (Cookie cookie : cookies) { if (name.equals(cookie.getName())) { return cookie; } } } return null; }Copy the code

Loops through all cookies to get the specified cookie.

Add a Cookie

public static boolean addCookie(HttpServletResponse response, String name, String value,
            int maxAge) {
    if (StringUtils.isBlank(name) || StringUtils.isBlank(value)) {
        return false;
    }
    Cookie cookie = new Cookie(name.trim(), value.trim());
    if (maxAge <= 0) {
        maxAge = Integer.MAX_VALUE;
    }
    cookie.setMaxAge(maxAge);
    cookie.setPath("/");
    response.addCookie(cookie);
    return true;
}
Copy the code

This addition is simple.

Delete the Cookie

public static boolean removeCookie(HttpServletRequest request, HttpServletResponse response, String name) { if (StringUtils.isBlank(name)) { return false; } Cookie[] cookies = getCookies(request); if (null ! = cookies) { for (Cookie cookie : cookies) { if (name.equals(cookie.getName())) { cookie.setValue(null); cookie.setMaxAge(0); cookie.setPath("/"); response.addCookie(cookie); return true; } } } return false; }Copy the code

Delete cookies, set value to null, and set max-age to 0.

Pay attention to

The editing operation is the same as the deletion operation, but it should be noted that all attributes except value and maxAge, such as name, path and domain, must be identical with the original Cookie when modifying or deleting cookies. Otherwise, the browser will not overwrite the cookies as two different cookies, resulting in the modification and deletion failure.

Recommended reading

Dry goods: 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Interview: the most comprehensive ali advanced Java interview questions in history

Interview: The most complete Spring interview questions in history

Tutorial: The most complete Spring Boot complete video tutorial

Books: 15 must-read books for advanced Java architects

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.