getSession has two methods, one with parameters and one without parameters.
getSession(), like getSession(true), gets the session object associated with the request object, and returns a new session if there is no session.
getSession(false) also returns a session object associated with the request object, but returns null if there is no session.
Definition in Java EE 5 API:
HttpSession getSession(boolean create)
HttpSession
associated with this request or, if there is no current session and
create
is true, returns a new session.
create
is
false
and the request has no valid
HttpSession
, this method returns
null
.
When the Jsp page submits the form to the servlet, it returns a session instead of null when calling getSession(false) in the servlet.
The reason is that the Jsp page will create a session object by default, that is, <@page session="true">, the default is true. If the session is manually set to false, the jsp will not create a session object, and the page cannot directly use the session object. . This is submitted to the servlet, and the method getSession(false) returns null.
Original post address: http://blog.csdn.net/wing32/article/details/5618106