getSession method of HttpServletRequest interface

Regarding the getSession method of the HttpServletRequest interface, when the page submits a request to the servlet, it returns a session object instead of null with getSession(false).

 

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)

Returns the current  HttpSession  associated with this request or, if there is no current session and create  is true, returns a new session.
If  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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326599232&siteId=291194637