Cookie and Session

Keywords: html5 Vue.js Ajax

Scope of four domain objects in Java Web

Application (application domain)
After the deployment of the whole project, there will only be one application domain object. All clients access the same application domain object, and all dynamic resources of the project also share the same application domain object

Request (request domain)
Each request has a request domain object. When the request ends, the corresponding request domain object is destroyed

Session (session domain)
The session domain starts when the client connects to the server and ends when the client closes. All requests in the whole process are in the same session domain; Different clients cannot share the session domain

Page (page field)
The life cycle of page (page domain) refers to the execution period of the page. The object stored in the page field is only accessible to the page where it is located

Why session control?

Maintaining the user login status means that after the user logs in, the user's login status will be saved in the server. When the user subsequently accesses other dynamic resources (Servlet or Thymeleaf) in the project, it can judge whether he has logged in at present. All requests that occur during the process from user login to user logout are actually within the scope of one session

[control]
Process control: control the process of program execution.

Session control: control the session between the browser and the server [for example, restart the browser and still expect the session to be valid, use (session control)]

Cookie

A Cookie is actually a small piece of information [key value] saved by the server on the browser, which is used by the server to distinguish different browsers

Role of cookies

  1. Store data in browser
  2. Carry the data stored in the browser to the server

How cookies work

  • The browser sends a request to the server
  • The server creates a Cookie object [card] and saves user information. Finally, the server sends the Cookie to the browser
  • When the browser sends the request again in the future, it will carry the Cookie object
  • The server distinguishes different browsers through Cookie objects

Use of cookies

Cookie related API s
  • Create a cookie object (cookies can only save string data, and cannot save Chinese)
new Cookie(String name,String value);
  • Write the cookie back to the browser
response.addCookie(cookie); 
  • Get all cookies brought by the browser:
request.getCookies() ; //Get all cookie objects. Is an array. The target cookie is obtained according to the key during development
  • cookie API
cookie.getName() ; //Returns the key set in the cookie
cookie.getValue(); //Returns the value set in the cookie
ServletDemo01 code

Create Cookie data in ServletDemo01 and respond to the client

public class ServletDemo01 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. Create a cookie object to store key value pairs
        Cookie cookie = new Cookie("cookie-message","hello-cookie");

        //2. Add the cookie to the response
        //The bottom layer is carried to the browser through a response header called "set cookie"
        response.addCookie(cookie);
    }
}
ServletDemo02 code for obtaining Cookie data
public class ServletDemo02 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. Remove the cookie from the request
        //The bottom layer is carried by a request header called "Cookie"
        Cookie[] cookies = request.getCookies();

        //2. Traverse every cookie
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                //name of matching cookie
                if (cookie.getName().equals("cookie-message")) {
                    //It's the cookie we want
                    //We get its value
                    String value = cookie.getValue();
                    System.out.println("stay ServletDemo02 Get in str The value of is:" + value);
                      	/*Modify the specified Cookie value
				        cookie.setValue("level2");
				        //Respond the modified Cookie object to the browser
				        response.addCookie(cookie);
				        break;
				        Method 2: [directly create a Cookie with the same name and overwrite it]
						Cookie cookie = new Cookie("level","level3");
						response.addCookie(cookie);*/
                }
            }
        }
    }
}
Timeliness of cookies

Posted by monkeytooth on Sat, 16 Oct 2021 20:05:02 -0700