Hello, 

I wrote a 'LoginServlet' which forwards a request to 'forwardTest.jsp'.
If any exception is thrown inside 'forwardTest.jsp', I pass error context 
to the 'error.jsp' page.
Before writing a real program, I wrote a very simple test program, which 
were attached with this email.

Everything works fine except the error page.
Since I needed to pass some information of 'forwardTest.jsp' to 'error.jsp',
I used session to store the informtion as an object named 'context'. Then,
'error.jsp' will call 'session.getAttribute("context")' to get 'context'.
However, in some cases, session of 'error.jsp' is different from session of
'forwardTest.jsp' and 'LoginServlet', so 'session.getAttribute("context")' 
returns null, which is not good and I don't know why 'error.jsp' uses
a different session in some cases.

How I can solve this situation?
(I think I can pass 'context' object from 'forwardTest.jsp' to
'error.jsp' using 'request.setAttribute("context", context)' but I am not
sure whether this kind of approach is valid with other JSP/Servlet containers.)

Thank you,

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
The following is my approaches:
(Please, read the program at the end of this email first...)

------ with LINE (A) ---
doGet : 5koh27xub1
ForwardTest.jsp : 5koh27xub1
Error.jsp : 78h6nzxuc1         ---> why does this page uses a different
session?
Context is null
------ pressed Ctrl-R ----
doGet : 1fpwtdxux1
ForwardTest.jsp : 1fpwtdxux1
Error.jsp : yg3in1xux2
Context is null

----- removed LINE (A), compiled and deployed, and restarted Tomcat ---
doGet : 3weph8xx11
ForwardTest.jsp : 3weph8xx11
Error.jsp : nmsi0cxx31        ---> This is not very good situation, either.
Context is null                    Without LINE (A), only the first try will
fail. 
------ pressed Ctrl-R ----         And I don't know why.
doGet : nmsi0cxx31 
ForwardTest.jsp : nmsi0cxx31
Error.jsp : nmsi0cxx31       
Context is java.lang.Object@24c4a3
------ pressed Ctrl-R ----
doGet : nmsi0cxx31
ForwardTest.jsp : nmsi0cxx31
Error.jsp : nmsi0cxx31
Context is java.lang.Object@69c82e

------ instead of accessing LoginServlet, ------
------ I typed ../jsp/test/forwardTest.jsp -----
------ and this worked as I had expected -----
ForwardTest.jsp : upfvmyxyh1
** new session
Error.jsp : upfvmyxyh1       
Context is java.lang.Object@1fc2fb
------ pressed Ctrl-R ----
ForwardTest.jsp : upfvmyxyh1
Error.jsp : upfvmyxyh1
Context is java.lang.Object@3fea1d



-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
                     SOURCE CODE
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

-=-=-=-=-=-=-=-= LoginServlet.java -=-=-=-=-=-=-=-=-=-=-=-=
package whatever;

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

public class LoginServlet extends HttpServlet
{
    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
                      throws ServletException, IOException
    {
        req.getSession().invalidate();                      // LINE (A)

        HttpSession session = req.getSession();
        session.setAttribute("user", "something");
        
        System.out.println("doGet : " + session.getId());

        try
        {
            forward(req, resp, "/jsp/test/forwardTest.jsp");
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    protected void forward(HttpServletRequest req, 
                           HttpServletResponse resp, 
                           String file) throws ServletException, IOException
    {
        getServletConfig().getServletContext()
                          .getRequestDispatcher(file)
                          .forward(req, resp);
    }
}

-=-=-=-=-=-=-=-=-=-= FORWARD.JSP -=-=-=-=-=-=-=-=-=-=-=-=-=
<%@ page language="java" 
         import="java.util.*" 
         errorPage="error.jsp"
%>
<%
    System.out.println("ForwardTest.jsp : " + session.getId());
    
    try  {
        if (session.isNew() ) 
            System.out.println("** new session");
    } catch (Exception e)  {
        System.out.println("** illigal state");
    }
      
    Object context = new Object();
    session.setAttribute("context", context);
        
    if (true) 
       throw new com.thoughtworks.util.SystemException(
           "This exception will be handled by error.jsp"); 
%>

-=-=-=-=-=-=-=-=-=-= ERROR.JSP -=-=-=-=-=-=-=-=-=-=-=-=-=-=
<%@ page isErrorPage="true" %>

<%   System.out.println("Error.jsp : " + session.getId());

     Object context = session.getAttribute("context");                     
     System.out.println("Context is " + context);
%>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Reply via email to