Jsp - state management

1. The statelessness of the http protocol:

  Stateless means that when the browser sends a request to the server, the server responds to the client request.

  But when the same browser sends the request to the server again, the server doesn't know that it is the same browser.

  Simply put, the server will not remember you, so it is a stateless protocol.

2. Cookie overview:

  ①Two mechanisms for saving the user's state: Session, Cookie

  ②What are cookies?

   Cookie: The Chinese name is "small cookie", which is a series of text information stored on the client by the Web server.

   Typical application 1: Determine whether a registered user has logged in to a website.

   Typical application two: "shopping cart" processing.

   The application of cookies in life:

        The system will automatically record the videos that have been viewed;

        Remember user name and password to realize automatic login function;

  ③The role of cookies:

    a. Tracking of specific objects;

    b. Save the user's web browsing records and habits;

    c. Simplified login;

   Security risk: it is easy to leak user information;

3. Create and use cookies in Jsp:

  ①Create a Cookie object

    Cookie newCookie = new Cookie(String key ,Object value);

  ②Write the Cookie object

    response.addCookie(newCookie);

  ③Read the Cookie object

    Cookie[ ] cookies = request.getCookie();

  Common method:

   ①void setMaxAge(int expiry) //Set the validity period of the cookie, in seconds;

   ②void setValue(String value) //Assign the cookie after the cookie is created;

   ③String getName() //Get the name of the cookie;

   ④String getValue() //Get the value of the cookie;

   ⑤int getMaxAge() //Get the valid time of the cookie, in seconds;

 

4. Comparison of Session and Cookie:

  

 

    

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340024&siteId=291194637