Relationship between JSP/Servlet sessions

Discussions

Web tier: servlets, JSP, Web frameworks: Relationship between JSP/Servlet sessions

  1. Relationship between JSP/Servlet sessions (6 messages)

    Hi. I have a JSP application, in which I set a session attribute:
    <%
    session.setAttribute("attName",att);
    %>

    Then I have this servlet which has to retrieve the same attribute, but it finally doesn´t (what I get is "null"). The code for the servlet is (inside the doGet() or doPost() method):
    HttpSession session = request.getSession();
    String att = (String) session.getAttribute("attName");

    My question is: what's the relationship between Jsp and servlet sessions? In the servlet, should I get the session from anywhere else but the request?

    P.S. If I try to retrieve the attribute from another JSP:
    <%
    String att = (String) session.getAttribute("attName");
    %>
    it works.

    Thanks.
  2. A JSP is always part of a session by default. To turn it off you must have:
     
    <%@ page session="false" %>

    at the top of your code. A session is active until the client quits the browser, expires or something crashes somewhere. Care must be taken regarding how you invoke your sessions and understanding how the servlet container may or may not use cookies to track the client.

    The API for session monitoring/maintainance is large, so you are probably better off doing a google search or grabbing a servlet book. I may be able to help with some specific questions about sessions but the topic is way to broad to cover it quickly.

    Hope it helps!
  3. Thanks, Francisco. I've spent the whole day on this and my only conclusion is that the session is lost when the servlet is called from the JSP. The way the servlet is called is a link:
    A href='http://www.foo.com/myapp/myservlet?param1=value1

    The application is running on IIS, which passes JSP/Servlet calls on to Tomcat.

    I cannot get the session, but I do get a Cookie named JSESSIONID, and with a value equal to the session in the JSP pages. I don´t know if I can get the session via this cookie. Is that possible?

    I would like to remark that session handling is working fine in JSP.

    Do these new pieces of information add anything new? Thanks in advance.
  4. Just one more thing to keep in mind when using cookies. In order to be able to share the session, clients must invoke the session consistenly. That is,

                 http://localhost/myapp/myservlet

    would not use the same session as:

                http://127.0.0.1/myapp/myservlet

    You can always print the session id as seen from the client and as seen from the server. Here's the syntax:

    System.out.println("Session id: " + session.getId() );

    If the ideas do not match, cookies are enabled and you account for the above then some is broken somwhere...

    Hope it helps!
  5. JSP and Servlet sessions are same. The Session object is same.
    I have never faced any problem of this nature : Setting session in JSPs, and those not being available in Servlets.
    I use Tomcat as the Servlet Engine.
  6. "JSP and Servlet sessions are same. The Session object is same. "

    That depends... Try the following: Build a JSP with one input field and a submit button. Then do the following:

    Create the JSP with following form action URL
    http://127.0.0.1:8080/MyApp/MyServlet

    1. Call your JSP
    http://localhost:8080/MyApp/MyJSP

    2. Populate the input field, set the value to the session and press submit.

    3. Try to read the input field's value from your servlet's session.

    You will get NULL POINTER ERROR. Because the cookies do not match. In this case the JSP and Servlet, despite the fact that they are talking to each other, do not share the same session object.
  7. A possible solution[ Go to top ]

    I'm the one who posted the first message. It seems that the problem is not the session itself, but communication between web applications. I have the JSP files in IIS, and the servlet file in Tomcat. Apparently, this results in two different web applications, the one with the JSP pages and the one with the servlet. So the sessions for each other are different.
    Having this in mind, now I have to find a way for both app to communicate, and I think the best way is via a special database built for this purpose.
    That's all, I just wanted to share with you the solution I found (well, not really found, someone told me in Tomcat-user mailing list), since many people seem to have this problem but no documentation can be found easily.
    Thanks for all your answers!