2021-02-18-session introduction and the difference between session and cookie

Session introduction and the difference between session and cookie

concept

  • Chinese translated into conversational meaning
  • The bottom layer is based on cookies
  • Key-value pair form
  • It can be used to store the value that the backend wants to store, and then it can be obtained on the front-end page
  • The default timeout time of the session is 30 minutes, and the default timeout time can be modified in the web.xml of the project

method

Method name effect
getId() Get SessionID
getCreationTime() Get the creation time of the session
getLastAccessedTime() Get the last operation time of the session
isNew() Determine whether to obtain a new session (new user)
invalidate() Invalidate the session
setAttribute(String s, Object obj) Add content
getAttribute(String s) Get content by key

The difference between session and cookie

Compared cookie session
Storage location Client browser or hard drive Server
Storage data type A cookie can only store one key-value value, and both are of string type Can save any type
Whether the client can operate Can be accessed through js on the html side of the browser Can't operate
Whether the server is operating Can operate Can operate
Create method Cookie(String name, String value) setAttribute(String s, Object obj)
Quantity stored Generally, a project can have up to 20 There is only one shared session for a single client
Disadvantage The client cannot write in if the cookie is disabled on the client. It is not safe to put data on the client. Occupy server resources, so there is less data in the session

Domain object (four attribute ranges)

  • appliaction: save on the server, all users can use
  • pageContext: only saved in one page, invalid after jump
  • request: only saved in one request, it is still valid after the server jumps
  • session: Save in a session, you can use it regardless of any jump, but you can't use it after opening a new browser

Guess you like

Origin blog.csdn.net/qq_41270550/article/details/113805174