Cookie-related instructions:
1. The cookie is created by the server side and then added to the HttpServletResponse to the client (browser).
2. Multiple cookie key-value pairs can be added.
3. The cookie consists of a key value name and a key value. The key-value names in "Same domain and path" cannot be duplicated, and a key-value pair with the same name as the key-value will overwrite the previous key-value pair with the same name.
4. When adding cookies, specify the domain of the cookie (SetPath), specifying the Duration (setmaxage).
4. After the server has created the cookie, it is submitted to the client, and then each request (HttpServletRequest) of the browser will carry a "cookie array".
5, SPRINGMVC has two ways to obtain: (1) in the controller through the annotation @cookievalue (key value name), get the specified cookie. (2) Obtain an array of cookies through the GetCookies method in HttpServletRequest, and then iterate through each of the cookie key-value pairs.
Session Related instructions:
1. The server will create a session (Request.getsession ()) based on the client's request (HttpServletRequest).
2, each session has a unique marked "SessionID", can be obtained through the. getId ().
3, as long as the browser is not closed, then according to the browser request (HttpServletRequest) created by the session is the same.
4, the session's default life cycle is "browser access to the server after the server based on access requests to create session" to "browser close."
Related Operation code:
//read the cookie array, then iterate over each cookie Public voidshowcookies (HttpServletRequest request) {cookie[] cookies= Request.getcookies ();//find the cookie array based on the requested data if(NULL==cookies) {//If there is no cookie arraySystem.out.println ("No Cookies"); } Else { for(Cookie cookie:cookies) {System.out.println ("CookieName:" +cookie.getname () + ", Cookievalue:" +Cookie.getvalue ()); } } } //Create a cookie and add the new cookie to the Response object response. Public voidAddcookie (httpservletresponse response) {Cookie Cookie=NewCookies ("Name_test", "value_test");//Create a new cookieCookie.setmaxage (5 * 60);//set the presence time to 5 minutesCookie.setpath ("/");//Setting ScopeResponse.addcookie (cookie);//adds a cookie to the response cookie array to return to the client } //Modify the cookie to modify it according to the name of a cookie (not just name to be consistent with the cookie being modified, path, domain must also be consistent with the cookie being modified) Public voidEditcookie (httpservletrequest request,httpservletresponse response) {cookie[] cookies=request.getcookies (); if(NULL==cookies) {System.out.println ("No Cookies"); } Else { for(Cookie cookie:cookies) {//Modify related data if the same cookie is found with the specified cookiename during iteration if(Cookie.getname (). Equals ("Name_test") {cookie.setvalue ("New_value");//Modify ValueCookie.setpath ("/"); Cookie.setmaxage (10 * 60);//Modify Survival TimeResponse.addcookie (cookie);//Save the modified cookie to response and replace the old cookie with the same name Break; } } } } //Delete Cookies Public voidDelcookie (httpservletrequest request,httpservletresponse response) {cookie[] cookies=request.getcookies (); if(NULL==cookies) {System.out.println ("No Cookies"); } Else { for(Cookie cookie:cookies) {//If a cookie of the same name is found, the value is set to NULL, the survival time is set to 0, and the original cookie is replaced, which is equivalent to the deletion. if(Cookie.getname (). Equals ("Name_test") {cookie.setvalue (NULL); Cookie.setmaxage (0); Cookie.setpath ("/"); Response.addcookie (cookie); Break; } } } }
SPRINGMVC Operation Cookie and session