Title: Servlet regains control after execution of .Forward()
Just put a return after the call to forward.
 
The forward call is not magical.  It's just a method call and when the method completes, the thread continues to System.out.println("BUGGER ME! I AM HERE AFTER ALL");  The only thing the spec calls for is that the response be committed and closed by the container before the forward method returns.  The only way (from an interface perspective) to skip that last System.out call without is to throw an exception in the forward call that's declared in the throws clause of the service method.  The container could then interpret interpret the is exception as the result of a successful forward and basically ignore it but the spec does not call for this and to my knowledge, no server that I does this... they'd have to use the IOException or ServletException in an unorthodox way to accomplish this...  In any case, just put a return after the forward.
 
 
The following is an excerpt from Servlet2_2-spec.pdf
 
8.4 Forward

The forward method of the RequestDispatcher interface may only be called by the calling

servlet if no output has been committed to the client. If output exists in the response buffer that has

not been committed, it must be reset (clearing the buffer) before the target servlet�s service

method is called. If the response has been committed, an IllegalStateException must be

thrown.

The path elements of the request object exposed to the target servlet must reflect the path used to

obtain the RequestDispatcher. The only exception to this is if the RequestDispatcher

was obtained via the getNamedDispatcher method. In this case, the path elements of the

request object reflect those of the original request.

Before the forward method of the RequestDispatcher interface returns, the response must

be committed and closed by the servlet container.


 -----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Grant Doran
Sent: Thursday, November 09, 2000 6:52 PM
To: Orion-Interest
Subject: Servlet regains control after execution of .Forward()

Hi,

(Orion 1.3.8, Windows NT 4, JDK1.3)
This should not be possible, but it is happening. After executing the forward() method, control IS returning to the forwarding servlet. I have checked the archives and found that this has been an issue in previous versions of orion.

Here is the offending fragment:
---------------------------------------------------
if (condition XXX)
{
  request.getSession().setAttribute("username",username);
  request.getSession().setAttribute("password",password);
  System.out.println("[GATEWAYSERVLET]User has now logged in and is being redirected to reception");
  request.getSession().setAttribute("LoginFailure","false");
  getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
}
else
{
  System.out.println("[GATEWAYSERVLET]Authentication failure. Username:[" + username + "] Password:[" + password + "]");

  request.getSession().setAttribute("LoginFailure","true");
  getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}
System.out.println("BUGGER ME! I AM HERE AFTER ALL");
----------------------------------------------------------------

Here is the output
----------------------------------------------------------------
[GATEWAYSERVLET]User has now logged in and is being redirected to reception
[INDEX.JSP]Session is established. UserId = gdoran. Printing frames with menus etc.
BUGGER ME! I AM HERE AFTER ALL
----------------------------------------------------------------

How can this be? Execution of the servlet(thread) is supposed to cease immediately and control handed to the target.

Am I missing something?

While I'm at it, does anyone know where I can obtain a good pattern example of JSP/Servlet based authentication, with a login page for any unauthenticated requests. I am pushing all requests through a single controller servlet and checking their session object for the required attributes, but I seem to be having problems with retrieving the session. Sometimes the attributes are there, sometimes they aren't.

Any guidance would be greatly appreciated.

Grant Doran
Principle Architect
iLaunch inc.
(02) 89257055
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>


 


Reply via email to