Geeks Talk

Prepare for your Next Interview




Regarding Servlets

This is a discussion on Regarding Servlets within the JSP forums, part of the Web Development category; hello can any one tell me what is the difference between request.getSession() and request.getSession(true). thanks in advance samineni...


Go Back   Geeks Talk > Web Development > JSP

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 06-07-2007
Contributing Member
 
Join Date: May 2007
Location: Chennai
Posts: 34
Thanks: 0
Thanked 5 Times in 4 Posts
samineni6 is on a distinguished road
Smile Regarding Servlets

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

thanks in advance
samineni
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-08-2007
Junior Member
 
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
trycatch is on a distinguished road
Re: Regarding Servlets

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....
Reply With Quote
  #3 (permalink)  
Old 06-08-2007
Contributing Member
 
Join Date: May 2007
Location: Chennai
Posts: 34
Thanks: 0
Thanked 5 Times in 4 Posts
samineni6 is on a distinguished road
Re: Regarding Servlets

hello trycatch
thanks for ur reply
but i ask the difference between getSession() and getSession(true) not
for getSession(false)
Reply With Quote
  #4 (permalink)  
Old 06-08-2007
Junior Member
 
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
trycatch is on a distinguished road
Re: Regarding Servlets

hi samineni6,
What i observe is,getSession() behave like getSession(false). in both condition it will create a new session..
Reply With Quote
  #5 (permalink)  
Old 06-08-2007
Contributing Member
 
Join Date: May 2007
Location: Chennai
Posts: 34
Thanks: 0
Thanked 5 Times in 4 Posts
samineni6 is on a distinguished road
Re: Regarding Servlets

no thats wrong yar
Reply With Quote
  #6 (permalink)  
Old 06-08-2007
Junior Member
 
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
trycatch is on a distinguished road
Re: Regarding Servlets

what the right answer...mentioned for more enhanced reply..
If you know you should send the appropriate thread....
Reply With Quote
  #7 (permalink)  
Old 06-08-2007
Junior Member
 
Join Date: Mar 2007
Location: Bangalore
Posts: 10
Thanks: 0
Thanked 2 Times in 2 Posts
eswar_ambati is on a distinguished road
Re: Regarding Servlets

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
Reply With Quote
  #8 (permalink)  
Old 08-15-2007
Junior Member
 
Join Date: May 2007
Location: Bangalore
Posts: 19
Thanks: 0
Thanked 1 Time in 1 Post
sweet_person is on a distinguished road
Re: Regarding Servlets

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?
Reply With Quote
  #9 (permalink)  
Old 08-16-2007
Junior Member
 
Join Date: Mar 2007
Location: Bangalore
Posts: 10
Thanks: 0
Thanked 2 Times in 2 Posts
eswar_ambati is on a distinguished road
Re: Regarding Servlets

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.
Attached Files
File Type: zip sessionex.zip (465.0 KB, 9 views)

Last edited by eswar_ambati : 08-16-2007 at 02:11 AM.
Reply With Quote
  #10 (permalink)  
Old 08-16-2007
Junior Member
 
Join Date: May 2007
Location: Bangalore
Posts: 19
Thanks: 0
Thanked 1 Time in 1 Post
sweet_person is on a distinguished road
Re: Regarding Servlets

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.
Reply With Quote
  #11 (permalink)  
Old 08-16-2007
Junior Member
 
Join Date: May 2007
Location: Bangalore
Posts: 19
Thanks: 0
Thanked 1 Time in 1 Post
sweet_person is on a distinguished road
Re: Regarding Servlets

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.
Reply With Quote
  #12 (permalink)  
Old 08-17-2007
Junior Member
 
Join Date: Mar 2007
Location: Bangalore
Posts: 10
Thanks: 0
Thanked 2 Times in 2 Posts
eswar_ambati is on a distinguished road
Re: Regarding Servlets

"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
Reply With Quote
  #13 (permalink)  
Old 08-17-2007
Junior Member
 
Join Date: Jul 2007
Location: Bangalore, India
Posts: 12
Thanks: 0
Thanked 3 Times in 2 Posts
JaiBharath is on a distinguished road
Re: Regarding Servlets

hey dudes,

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

Cheers,
Jai
Reply With Quote
  #14 (permalink)  
Old 08-20-2007
Junior Member
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
vravindran is on a distinguished road
Re: Regarding Servlets

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.
Reply With Quote
  #15 (permalink)  
Old 08-22-2007
Junior Member
 
Join Date: Nov 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
karthik is on a distinguished road
Re: Regarding Servlets

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.
Reply With Quote
  #16 (permalink)  
Old 11-19-2007
Junior Member
 
Join Date: May 2007
Location: Delhi
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Brajendra is on a distinguished road
Re: Regarding Servlets

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.
Reply With Quote
Reply

  Geeks Talk > Web Development > JSP


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
diif b/w servlets and jsp vmshenoy Java 9 03-25-2008 03:56 AM
Help regarding JSP and servlets neelim Java 8 05-29-2007 03:49 AM
Book for JSP and Servlets samineni6 JSP 3 05-17-2007 08:26 AM
number of servlets in a web container JobHelper Java 3 03-06-2007 09:18 AM
Testing for Applets and Servlets JobHelper Testing Issues 0 12-17-2006 02:45 AM


All times are GMT -4. The time now is 01:39 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved