Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between request.getSession().getId() and request.getSession(false)?

Tags:

What do these calls actually mean in terms of session?

System.out.println("print1: "+request.getSession().getId()); System.out.println("print2: "+request.getSession(false)); 

OUTPUT

print1: D94146A347D95563186EB7525726336B print2: org.apache.catalina.session.StandardSessionFacade@d52411 
like image 726
lata Avatar asked Dec 20 '12 10:12

lata


People also ask

What is request getSession false?

request. getSession(false) will return current session if current session exists. If not, it will not create a new session. Follow this answer to receive notifications.

What is the default argument of getSession () method?

The default behavior of this method is to return getSession() on the wrapped request object. Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.

What is session getId?

getId() Returns a string containing the unique identifier assigned to this session. long. getLastAccessedTime() Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT, and marked by the time the container received the request.

What is the significance of getSession true )?

- getSession(true) will check whether a session already exists for the user. If yes, it will return that session object else it will create a new session object and return it.


2 Answers

HttpSession session = request.getSession(); Inside the service method we ask for the session and every thing gets automatically, like the creation of the HttpSession object. There is no need to generate the unique session id. There is no need to make a new Cookie object. Everything happens automatically behind the scenes.

As soon as call the method getSession() of the request object a new object of the session gets created by the container and a unique session id generated to maintain the session. This session id is transmitted back to the response object so that whenever the client makes any request then it should also attach the session id with the requsest object so that the container can identify the session.

request.getSession(false) : This method will check whether Session already existed for the request or not. If it existed then it will return the already existed Session. If Session is not already existed for this request then this method will return NULL, that means this method says that the request does not have a Session previously.

like image 125
Manish Nagar Avatar answered Oct 12 '22 13:10

Manish Nagar


In short-

request.getSession().getId() - returns a string containing the unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.

request.getSession(false) - return session object or null if there's no current session.

like image 30
Subhrajyoti Majumder Avatar answered Oct 12 '22 13:10

Subhrajyoti Majumder