Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between request.getSession() and request.getSession(true)

I understand the difference between request.getSession(true) and request.getSession(false). But request.getSession() & request.getSession(true) look very similar!

Both "return the current session associated with this request", but differ in:

request.getSession():

"or if the request does not have a session, creates one"

request.getSession(true):

"if there is no current session, returns a new session"

I don't understand the difference between them, is it that (if none exists) they create a new session but the first one doesn't return it but the second one does?

Source: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

Edit:

Someone tagged/marked my question as duplicate even though it isn't. I will explain why.

I have explicitly asked for the difference between request.getSession() & request.getSession(true) and NOT between request.getSession(true) & request.getSession(false)! I have stated , again explicitly, that I already understand the difference b/w ..(true) & ..(false).

The question linked as a possible duplicated of of asks about the difference b/w ..(true) & ..(false) and not ..(true) & ..()

like image 949
Ali Khan Avatar asked Jun 22 '15 12:06

Ali Khan


People also ask

What is request getSession ()?

getSession() returns the valid session object associated with the request, identified in the session cookie that is encapsulated in the request object. Calling the method with no arguments creates a session if one does not exist that is associated with the request.

What is 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.

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 the getSession () method with true parameter will returned?

The getSession() method with 'true' as its parameter [ getSession(true) ] it will return the appropriate session object when. getSession() method is a part of HTTP which is in itself a stateless protocol. It doesn't provides a way for a server to recognize which client is using what part of the application.


2 Answers

request.getSession() will return a current session. if current session does not exist, then it will create a new one.

request.getSession(true) will return current session. If current session does not exist, then it will create a new session.

So basically there is not difference between both method.

request.getSession(false) will return current session if current session exists. If not, it will not create a new session.

like image 113
Ranjitsinh Avatar answered Sep 30 '22 16:09

Ranjitsinh


request.getSession() is just a convenience method. It does exactly the same as request.getSession(true).

like image 30
Jan Avatar answered Sep 30 '22 14:09

Jan