Several methods of obtaining request.getSession() in java

Recently, sessions are used to store and obtain user login information. During use, sessions often fail to be obtained.
So I checked the relevant information for several situations of request.getSession() as a knowledge reserve.
In the javaweb project, the use of request.getSession() is generally to store information in the session or obtain information from the session.
Generally, there are three parameter setting methods:

  1. request.getSession()
    This is a common way to get the session from the current request. If the session cannot be obtained, a session will be automatically created and the newly created session will be returned; if it is obtained, the obtained session will be returned;
  2. request.getSession(true)
    This method is the same as the first one, except that a true parameter is added to tell it to automatically create a session when it cannot be obtained;
  3. The difference between request.getSession(false)
    and the above two methods is that when the session cannot be obtained, the session will not be created automatically, but null will be returned.

During use, generally
when you want to store in the session, use request.getSession(), and
when you want to get the information in the session, use request.getsession(false), and check whether the session variable is null after getting it Judgment, and then proceed to the next step.
—————————————————
Original link

Guess you like

Origin blog.csdn.net/CrazyQiQi/article/details/103224569