Session value not passing in my servlet from my bean

Discussions

Web tier: servlets, JSP, Web frameworks: Session value not passing in my servlet from my bean

  1. Hello,

    The flow is like this. I have a login servlet which stores the values using session.putValue( "logoninfo") and subsequently I call a html which has a servlet and this servlet calls a JSP. The JSP has a servlet which does some action. The session is being got till my JSP and in the earlier servlet, but not in the last one.

    I am using in my servlet this code

    HttpSession session = req.getSession(true);
    System.out.println("session is active for a maximum time of : " + session.getMaxInactiveInterval());
    System.out.println("Session ID Value in UserAdminServlet is " + session.getId());
    System.out.println(" Is this a new Session " + session.isNew());
    LogonInfo userInfo = (LogonInfo) session.getValue("logonInfo");
    System.out.println("UserAdmin.session.LogonInfo.userid=" + userInfo.getUserId());
    luserid = userInfo.getUserId();

    and in my JSP this way. Well it is passing till my JSP. But when i hit my last servlet it is showing a null pointer exception and when i say HttpSession sessio = req.getSession() , then it creats a new session , though strictly passed on.

    All ideas welcome.

    Please write to me at prasathb at yahoo dot com . I appreciate all you reply. Session Guru's this is a test for you.
  2. If you use getSession(true) all the time, then you will always get a new session.

    Try using getSession(false) in your JSP - be sure to check for null before using it.
  3. Try using getSession(false) in your JSP - be sure to check for null before using it.

    I have tried it using getSession( false) and even getSession() too.

    it is failing everywhere. Assuming that the session created is null, how do i call my old session object.any idea.
    Your email address is appreciated. I can send you my code if you want also.

    Write to me at prasathb at yahoo dot com or balakrishnan dot prasath at sbc dot com

    Appreciate you reply.


    Prasath
  4. Map out your logic so the first servlet or JSP used by your client calls getSession(true) so you are sure to establish a new session between the browser and your server.

    In all subsequent uses of getSession, use getSession(false).
  5. Hi Tom. Documentation on the JavaSoft.com website suggests that it's okay to use getSession(true) in subsequent servlets and jsps. It indicates a new session will only be created if one does not already exist. However, there is a note there that I'd not noticed before, and perhaps this may be an answer to Prasath's problem:

    Note: to ensure the session is properly maintained, the servlet developer must call this method (at least once) before any output is written to the response.


    I have had intermittent trouble with sessions "dropping out" on one of my web sites; now I'm wondering if this might be an explanation for the cause.

    Regards,

    Gordon.
  6. The whole problem is b/c JSP is not supporting cookies that hold the sessionID though you are always holding it in a bean. So i wrote a Javascript to call my servlet in the function and called the same thorugh a BUTTON instead of a FORM ACTION = *.* and calling it as a SUBMIT.
    and I called it in the onClick and it worked fine. I solved the problem for which I have been struggling over 2 weeks and I am relieved. I am for sure that the BUTTON works with the HREF