tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Allen Hopkins <all...@eecs.berkeley.edu>
Subject Session gone after 2 forwards
Date Tue, 29 May 2001 22:45:19 GMT
I posted last week about losing my HttpSession after doing
two forwards, from one servlet to a second, and then to a third.
All 3 servlets are in the same context.  The second servlet
sees the same HttpSession as the first, but the third sees
a new one, in which the session attribute from the first servlet
is not available.

After my first post, a couple suggestions were made about
things not being in the same context, but I don't think that
applies.  This looks like a bug in Tomcat to me, (but, hey,
what do I know?:-)) so I'm posting again about it.

This only happens on the first access after a server restart,
or after the session's 30-minute expiration has run out.

Here's some information from each of the three servlets in turn, just
after a server restart:

--- servlet 1: ---
ServletContext = org.apache.tomcat.facade.ServletContextFacade@342cbf
session id = h3m9az5eg1
session isNew() = true
session getCreationTime() = 991174054485
session getLastAccessedTime() = 991174054485
servlet1 says myAttribute = boo!

--- servlet 2: ---
ServletContext = org.apache.tomcat.facade.ServletContextFacade@342cbf
session id = h3m9az5eg1
session isNew() = false
session getCreationTime() = 991174054485
session getLastAccessedTime() = 991174054485
servlet2 says myAttribute = boo!

--- servlet 3: ---
ServletContext = org.apache.tomcat.facade.ServletContextFacade@342cbf
session id = vju2fx5eg2
session isNew() = true
session getCreationTime() = 991174064281
session getLastAccessedTime() = 991174064281
servlet3 says myAttribute = null

---------------------------------

... Then on a Reload of the web page, you see that the session makes
it to the third servlet OK:

--- servlet 1: ---
ServletContext = org.apache.tomcat.facade.ServletContextFacade@342cbf
session id = vju2fx5eg2
session isNew() = false
session getCreationTime() = 991174064281
session getLastAccessedTime() = 991174064281
servlet1 says myAttribute = boo!

--- servlet 2: ---
ServletContext = org.apache.tomcat.facade.ServletContextFacade@342cbf
session id = vju2fx5eg2
session isNew() = false
session getCreationTime() = 991174064281
session getLastAccessedTime() = 991174235758
servlet2 says myAttribute = boo!

--- servlet 3: ---
ServletContext = org.apache.tomcat.facade.ServletContextFacade@342cbf
session id = vju2fx5eg2
session isNew() = false
session getCreationTime() = 991174064281
session getLastAccessedTime() = 991174235759
servlet3 says myAttribute = boo!

---------------------------------
After 30 minutes, a Reload results in output that looks like the first
case, except the session IDs (and of course the times) have changed.
Servlets 1 & 2 have the same session ID.

Here's the source code for the three very simple servlets.  Most of
the code is involved in accumulating the output string and passing it
along to the third servlet (via the HttpServletResponse) for output.

---------------------------------
package gsrc.workshop;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet1 extends HttpServlet {

    public void service(HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {

        String message       = new String();
        ServletContext sc    = getServletContext();
        RequestDispatcher rd =
sc.getRequestDispatcher("/servlet/TestServlet2");           HttpSession
httpSession = req.getSession();
        
        httpSession.setAttribute("myAttribute", new String("boo!"));

        message += "--- servlet 1: ---<br>";
        message += "ServletContext = " + sc + "<br>";
        message += "session id = " + httpSession.getId() + "<br>";
        message += "session isNew() = " + httpSession.isNew() + "<br>";
        message += "session getCreationTime() = "
                   + httpSession.getCreationTime() + "<br>";
        message += "session getLastAccessedTime() = "
                   + httpSession.getLastAccessedTime() + "<br>";
        message += "servlet1 says myAttribute = "
                   + httpSession.getAttribute("myAttribute") + "<br>";
        req.setAttribute("message", message);

        rd.forward(req, res);
    }
}

---------------------------------
package gsrc.workshop;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet2 extends HttpServlet {

    public void service(HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {

        String message       = (String)req.getAttribute("message");
        ServletContext sc    = getServletContext();
        RequestDispatcher rd =
sc.getRequestDispatcher("/servlet/TestServlet3");           HttpSession
httpSession = req.getSession();

        message += "<br>--- servlet 2: ---<br>";
        message += "ServletContext = " + sc + "<br>";
        message += "session id = " + httpSession.getId() + "<br>";
        message += "session isNew() = " + httpSession.isNew() + "<br>";
        message += "session getCreationTime() = "
                   + httpSession.getCreationTime() + "<br>";
        message += "session getLastAccessedTime() = "
                   + httpSession.getLastAccessedTime() + "<br>";
        message += "servlet2 says myAttribute = "
                   + httpSession.getAttribute("myAttribute") + "<br>";
        req.setAttribute("message", message);

        rd.forward(req, res);
    }
}

---------------------------------
package gsrc.workshop;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet3 extends HttpServlet {

    public void service(HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {

        String message          = (String)req.getAttribute("message");
        ServletContext sc       = getServletContext();
        HttpSession httpSession = req.getSession();

        message += "<br>--- servlet 3: ---<br>";
        message += "ServletContext = " + sc + "<br>";
        message += "session id = " + httpSession.getId() + "<br>";
        message += "session isNew() = " + httpSession.isNew() + "<br>";
        message += "session getCreationTime() = "
                   + httpSession.getCreationTime() + "<br>";
        message += "session getLastAccessedTime() = "
                   + httpSession.getLastAccessedTime() + "<br>";
        message += "servlet3 says myAttribute = "
                   + httpSession.getAttribute("myAttribute") + "<br>";

        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("<html><body>");
        out.println(message);
        out.println("</body></html>");
    }
}

BTW, tomcat 3.2.1, apache 1.3.19, Solaris 2.8, Sun Ultra 60.

Thanks for your smarts.

-Allen Hopkins

Mime
View raw message