Prepare for your Next Interview
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...
|
|||
|
hello
can any one tell me what is the difference between request.getSession() and request.getSession(true). thanks in advance samineni |
| Sponsored Links |
|
|||
|
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.... |
|
|||
|
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 |
|
|||
|
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? |
|
|||
|
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. Last edited by eswar_ambati : 08-16-2007 at 02:11 AM. |
|
|||
|
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.
|
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |