GeeCON Prague 2014*
The moose likes Servlets and the fly likes dispatcher.forward: sometimes it does not work! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "dispatcher.forward: sometimes it does not work!" Watch "dispatcher.forward: sometimes it does not work!" New topic
Author

dispatcher.forward: sometimes it does not work!

Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
I want to go back to the login page if the user forgets to enter either his email address or his password.
I remember it worked very well yesterday with tomcat under Win2k.
How come it does not work today. I don't understand the problem. Is it a bug or something?
The part of the servlet code is attached here:
String emailAddress=request.getParameter("emailAddress");
String password=request.getParameter("password");
if((emailAddress==null)||(password==null) )
{ gotoPage("/jsp/myproject/login.jsp", request, response);
}
private void gotoPage(String address,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{ RequestDispatcher dispatcher
=getServletContext().getRequestDispatcher(address);
dispatcher.forward(request, response);
}
james edwin
Ranch Hand

Joined: Nov 22, 2001
Posts: 393
Well this world is full of surprises...one is which u said. code was working yesterday but not today .. strange things happen in this small world.
Well actually i will suggest just try to debug your code by putting System.out.println("**1**");
after every line then try to find out ,where it is getting stuck.
Hope this will help.
[ February 01, 2002: Message edited by: james edwin ]

Regards,
James
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Have you read the API for RequestDispatcher.forward? http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/RequestDispatcher.html
I'm not sure if this is the problem you are having, but it's fairly common. The API states:
forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.

So.... if you've already written enough data that the buffer is full and something has been sent to the client, the response is considered 'comitted' and forward will fail. Otherwise forward will succeed.
The problem is, that if forward ends up throwing an exception and you've already comited, you can't redirect to the error page either...
Dave
Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
The thing is I did not see any exception message from the tomcat dos window. The browser is just blank page, whose source I viewed, is just a couple of HTML code without real messages between the tags.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If you see this in your source <html><head></head></html> That is usually the browser making those for you, based on a 'null' response.
Manjunath Subramanian
Ranch Hand

Joined: Jul 18, 2001
Posts: 236
Check up in the logs
Gabriel Fox
Ranch Hand

Joined: Oct 17, 2001
Posts: 170
Hi Guys, the major problem with mis-using the forward()is that elaborated by David.Apart from that the forward() has always been very good.
So, Gene if U put all suggestions by ranchers together it should be well.
Note:
1.I will suggest U do the normal Javascript client-side validation ,if you are checking for empty field before the form is submitted.Hence,processing is reduced in your servlet-form handler.
2.Sometimes check the work-folder for Tomcat while debugging,(where you compiled/translated JSPs are kept).It gives U the true state of your
page while developing.
 
GeeCON Prague 2014
 
subject: dispatcher.forward: sometimes it does not work!