Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dispatcher.forward: sometimes it does not work!

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check up in the logs
 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic