Session introduction and use (incidentally cookie and sessionId)

What is Session?

A session management server object exists (tomcat as an example) of

What is a Cookie?

 Browser presence of a small text file (to save related data)

What is SessionId?

Used to obtain a sequence number of the Session answer (JVM + + actual value of the random id) id derived from the JVM's information calculation hardware

Session is created when?

HttpSession session = requset.getSession (); Create obtained, 
while () obtained by session.getId SessionId

Session and Cookie role?

Are used to save data of the user, has validity, it can be saved to a file by means of the corresponding

(Cookie in the use of the process is in memory, close the browser will disappear) unless the dump file

(Session presence server in a certain storage time)

The use of flow between the actual development

First, the browser requests the server, is generated by the session object on the server Request.getSession ()

HttpSession session =requset.getSession();

Second, obtaining the session object generated by sessionId 

String sessionId = session.getId()

Third, the sessionId returned to the browser, the cookie deposited into

  1. Get cookie
    Cookie[] cookies = request.getCookies();
  2. Of cookie operation (add, delete, change, check, deposit)
     
  3. Returned to the browser
    response.addCookie(cookie);

    Cookie related operations

    Create a way to get the cookie object: Cookie cookie = new Cookie (name, value);

    Cookie object has been acquired manner: Cookie [] cookies = req.getCookies ();

    Cookie object data operations: 
               1. Get Name: the Cookie.getName ()
               2, to obtain the value: cookie.getValue ()
               . 3, set the name: cookie.setName ()
               . 4, the set value: cookie.setValue ()

    Cookie, for example, residence time is provided: cookie.setMaxAge (365 * 24 * 60 * 60); in seconds

    Save Cookie object to the browser: response.addCookie (cookie);

    Set Cookie storage path: cookie2.setPath ( "/");

 

Data Update Session in

session.setAttribute save (the default method will save)
session.getAttribute get

 

 

 

Published 242 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41813208/article/details/103776211