JAVA Servlet-Cookie Object Notes

introduce

Cookies are small plain text information sent by the server to the browser. When the user visits the same web server in the future, the browser will send them to the server as they are. Cookies are generally used to identify user identities during online transactions, to prevent users from repeatedly entering names and passwords in occasions with low security requirements, to customize the homepage of portal websites, to place targeted advertisements, and so on. Multiple cookies can be created at the same time in the Servlet, but each site can only save up to 20 cookies

Cookie refers to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user's identity and track the session. (It can be called browser cache)

Instructions

create and send

@WebServlet("/sc04")
public class Cookie01 extends HttpServlet {
    
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        Cookie cookie=new Cookie("name","无锡");
        resp.addCookie(cookie);
    }
}

Obtain

@WebServlet("/sc04")
public class Cookie02 extends HttpServlet {
    
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
       Cookie []cookie=req.getCookies();
       if (cookie.length<=0||cookie==null)return;
       for (Cookie cookie1:cookie){
    
    
           String cname=cookie1.getName();
           String cvalue=cookie1.getValue();
       }
    }
}

Set cookie storage time

use

@WebServlet("/sc04")
public class Cookie03 extends HttpServlet {
    
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
       Cookie cookie=new Cookie("name","张三");
       cookie.setMaxAge(0);
       resp.addCookie(cookie);
    }
}
n value cookie.setMaxAge(n);
0 delete this cookie
-1 When it is a negative integer, it means that it is only stored in the browser, and the cookie will be destroyed when the browser is closed. The default is -1
30 When it is a positive integer, it means specify the survival time of the cookie, the unit is second, the cookie is stored in the local disk, and the cookie will not be destroyed even if the browser is closed

important point

1. The cookie is saved in the current browser and cannot be cross-browser.
2. There is a Chinese problem in the cookie, and the key value cannot be Chinese. If it must be Chinese, it is not recommended to use Chinese

//发送
String name="姓名"
String password=123456
name=URLEncoder.encode(name)
value=URLEncoder.encode(password)
Cookie cookie=new Cookie(name,value)
response.addCookie(cookie);

//获取
URLDecoder.decode(cookie.getName)
URLDecoder.decode(cookie.getValue)

3.
The cookie with the same name will overwrite the previous cookie

4. There is an upper limit to the storage quantity, and the upper limit is different for different browsers

5. The cookie size is limited, generally no more than 4kb

cookie path

Scenario 1 : Any resource of any project under the current server can obtain the Cookie object

//当前路径 为/s01
Cookie cookie=new Cookie("xxx","xxx")
cookie.setPath("/")
response.addCookie(cookie)

Scenario 2 : Resources under the current project can obtain Cookie objects

//当前路径 为/s02
Cookie cookie=new Cookie("xxx","xxx")
cookie.setPath("/s02")//可以不设置,默认就是当前路径
response.addCookie(cookie)

Scenario 3 : The specified project can get the Cookie object

//当前路径 为/s02
Cookie cookie=new Cookie("xxx","xxx")
cookie.setPath("/s03“)
response.addCookie(cookie)

Scenario 4 : Resources under the specified project can obtain Cookie objects

//当前路径 为/s02
Cookie cookie=new Cookie("xxx","xxx")
cookie.setPath("/s03/cook")//表示只有路径为/s03/cook才能拿到当前Cookie对象
response.addCookie(cookie)

Guess you like

Origin blog.csdn.net/LiuxXn/article/details/112184975