Results 1 to 16 of 16

Thread: Regarding Servlets

  1. #1
    Contributing Member
    Join Date
    May 2007
    Answers
    32

    Smile Regarding Servlets

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

    thanks in advance
    samineni


  2. #2
    Junior Member
    Join Date
    Apr 2006
    Answers
    7

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


  3. #3
    Contributing Member
    Join Date
    May 2007
    Answers
    32

    Re: Regarding Servlets

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


  4. #4
    Junior Member
    Join Date
    Apr 2006
    Answers
    7

    Re: Regarding Servlets

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


  5. #5
    Contributing Member
    Join Date
    May 2007
    Answers
    32

    Re: Regarding Servlets

    no thats wrong yar


  6. #6
    Junior Member
    Join Date
    Apr 2006
    Answers
    7

    Re: Regarding Servlets

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


  7. #7
    Junior Member
    Join Date
    Mar 2007
    Answers
    10

    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


  8. #8
    Junior Member
    Join Date
    May 2007
    Answers
    19

    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?


  9. #9
    Junior Member
    Join Date
    Mar 2007
    Answers
    10

    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 Attached Files
    Last edited by eswar_ambati; 08-16-2007 at 02:11 AM.

  10. #10
    Junior Member
    Join Date
    May 2007
    Answers
    19

    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.


  11. #11
    Junior Member
    Join Date
    May 2007
    Answers
    19

    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.


  12. #12
    Junior Member
    Join Date
    Mar 2007
    Answers
    10

    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


  13. #13
    Junior Member
    Join Date
    Jul 2007
    Answers
    12

    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


  14. #14
    Junior Member
    Join Date
    Jun 2006
    Answers
    3

    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.


  15. #15
    Junior Member
    Join Date
    Nov 2005
    Answers
    3

    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.


  16. #16
    Junior Member
    Join Date
    May 2007
    Answers
    1

    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.


  17.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact