Session management problem in JSP

Discussions

Web tier: servlets, JSP, Web frameworks: Session management problem in JSP

  1. Session management problem in JSP (3 messages)

    Hi !
    I have an EJB application with JSP as the client. Tha architecture is, JSP client, talking to stateless session bean and to BMP entity bean which interacts with oracle database. I manage session in the JSP using HttpSession and I store some session variables. The problem Iam facing is, when multiple user accesses the application, the data is getting shared between users. That is, I have five frames, in one frame I could see the data pertaining to my login, but in other frame, I could see the data of another logged in user and in the other frame, the data is of another, and it is not consistent. Sometimes, everything seems to be ok. And this problem persists only if multiple users are logged in. In each frame JSP page, I invoke JSP by the follwing systax :

    HttpSession s = request.getSession(true);

    s.putValue("planner",request.getParameter("lstPlanner"));
    s.putValue("dfcode",request.getParameter("lstDfcode"));
    s.putValue("supplier",request.getParameter("lstSupplier"));
    s.putValue("plannername",request.getParameter("hdPlannername"));

    String planner = String.valueOf(session.getValue("planner"));
    String dfcode = String.valueOf(session.getValue("dfcode"));
    String supplier = String.valueOf(session.getValue("supplier"));

    What could be the problem. What is the way to implement session to be client specific. Early reply is appreciated.
    Thanks
    AJP
  2. Session management problem in JSP[ Go to top ]

    THe code you posted should relate only to the current session.

    However, you have a race condition if you have multiple frames. Multiple frames = multiple threads in your servlet code. Hence, if the first frame is halfway through populating the session when the second one starts reading it (or even the other way round!) then the results will be unpredictable. Half the data, all the data, all the data etc.

    I don't see anything in the code you posted that would publish application scope things, which would be shared across all users.

    Try making a first page, which you access before all others, in which you populate the session information, and from there, kick off the frameset which contains the readers. See if that helps.

    Chz

    Tony
  3. Hi,

    We are using the session concept upon Login and storing it in Java Bean and calling it in our frame which has 3 frames. We don't see any problems so far.

    Try to call the manage ur session in your logon in a bean and continue. Good Luck.

    Prasath
  4. Session management problem in JSP[ Go to top ]

    From what you are saying, it appears your code is not
    thread-safe. It is generally a good idea to create an inner
    class to hold instance variables in your JSP's/Servlets,
    and to create an instance of this class in your JSP/Servlet
    from which you take your values. The reason for this is that non-local variables (eg, <%! instance variables %> ) share a common execution stack and are therefore shared between threads, whereas <% local variables %> declared inside methods have their own execution stack in each
    thread, and are by default thread-safe.

    Threading is a ubiqitous problem in web applications, but
    you can deal with it!

    Here's an example which may help you...

    //JAVA DECLARATION
    <%! //class to hold non-local variables
     
        class MyInnerClass {
            public String sString1;
            public String sString2;
        }
        
        //thread-safe method to print values
        void someMethod(MyInnerClass data, JSpWriter out) {

            out.println("STRING 1 = " + data.sString1);
            out.println("STRING 2 = " + data.sString2);

        }

    %>

    //JAVA CODE SNIPPET
    <%
        //instance to hold your non-local variables
        MyInnerClass data = new MyInnerClass();

        //store the parameter values in the local object
        data.sString1 = request.getParameter("sString1");
        data.sString2 = request.getParameter("sString2");
        
        //call method to print data - each thread holds it's
        //own copy of the "data" object.
        someMethod(data, out);

    %>

    You can contact me at martinwells at targetwise dot co dot uk

    Good luck, Martin.