# Web Development > JSP Regarding Servlets

## samineni6

hello 
can any one tell me
what is the difference between request.getSession()
and request.getSession(true).

thanks in advance
samineni

----------


## trycatch

hello,samineni6,

The basic difference is to get the session associated as part of the request.
If you say request.getSession(false),thats mean first you have to create the session(this is used as part of logout page),thats means it ensure, first you have to logon,after that only you can logout...

If you say request.getSession(true),thats mean you can get the old session reference as part of the request...OR if the session is not exist create a new one....(this is used as par of Login page)...

If i wrong,waiting for more enhanced reply....

----------


## samineni6

hello trycatch
thanks for ur reply 
but i ask the difference between getSession() and getSession(true) not 
for getSession(false)

----------


## trycatch

hi samineni6,
What i observe is,getSession()  behave like getSession(false). in both condition it will create a new session..

----------


## samineni6

no thats wrong yar

----------


## trycatch

what the right answer...mentioned for more enhanced reply..
If you know you should send the appropriate thread....

----------


## eswar_ambati

Hi 

There is no difference between request.getSession() and request.getSession(true). request.getSession() is a convinient way for request.getSession(true). There is a difference between request.getSession() and request.getSession(false).

If u use request.getSession(), if there is no session container will create a new session and returns session object.

If u use request.getSession(false), if there is no session container will not create new session and returns null.

Regards,
Eswar

----------


## sweet_person

Mr Ambati, i think you are wrong. request.getSession() creates a new session , irrespective of there is already a session or not.

but request.getSession(false) will not create a session if there is a session object  already in the container. if there is no session object in the container , now it wiil create a session object.

Please verify your answer?

----------


## eswar_ambati

Mr. Sweet_person, I dont agree with your comments. Please test the attached zip file. It contains sessionex.war.

In these two servlets, I used "request.getsession()". As per your comments it should create new session whether session is already present or not. 

In the firstservlet I added attributes to session. In the secondservlet i didnt do that. 

As per your comments, if u send request to firstservlet, it should create new session and 3 attributes should add. If u send request to secondservlet it should create new session, and it should display null values on the browser because i didnt set any attributes to the session in the secondservlet. 

But if u test the above two servlets, if u send request to the secondservlet also u can find the values set as attributes in the firstservlet. 

So, if session is already present, "request.getsession()" dont create session.

----------


## sweet_person

Mr Ambati Please read what i wrote and if you still have queries please send me the Servlet names and the files what you have tested. So that i can come to a precise decisssion.

----------


## sweet_person

Mr Ambati i have gone through the code of two servlets and find that you placed HttpSession session = request.getSession(); this code. But what you have to do is get session through reqest.getSession(false) in the SecondServlet,   and one thing you have to do in Second servlet is  dont set any attributes in the second servlet. 
Then you see the difference by first executing FirstServlet and SecondServlet what you get is the result what you are getting now.

Now close the browser and open a new window browser and execute the SecondServlet then you can find the difference what i am going to say. 
Thats all  i can say. If you still dont understand no one can help you.

----------


## eswar_ambati

"Mr Ambati, i think you are wrong. request.getSession() creates a new session , irrespective of there is already a session or not.

but request.getSession(false) will not create a session if there is a session object already in the container. if there is no session object in the container , now it wiil create a session object.

Please verify your answer?" - This is the post sent by you. check ur comments once..you told that request.getSession() will create session object irrespective of session present or not. In my code i used request.getSession() in both the servlets. but it didnt create new session object for second servlet after execution of first servlet. 

If u dont understand this never try to think of JAVA

----------


## JaiBharath

hey dudes,

In this discussion, I agree with eswar_ambati. I tested his code. Its the correct example for this discussion....

Cheers,
Jai

----------


## vravindran

request.getSession() will return the current session and if one does not exist, a new session will be cretaed.

request.getSession(true) will return the current session if one exists, if one doesn't exits a new one will be created.

So there is actually no difference between the two methods HOWEVER, if you use request.getSession(false), it will return the current session if one exists and if one DOES NOT exist a new one will NOT be cretaed.

----------


## karthik

hello

I want more java, java servlets, java swing, jsp, java bean, RMI examples programs and notes to known adout weblogic server tomcat server and application server and web server.

----------


## Brajendra

request.getSession()  and  request.getSession(true).... Both are same.
By default request.getSession()'s argument is true....... it means that when we get request.getSession() then if session object is exists then it is returned otherwise new one is created but in case of request.getSession(false) if we try to get session object using request.getSession(false) then if session object is exists then it is returned other wise null is returned.

----------

