Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to handle a RequestDispatcher if the page doesn't exist

 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm using a RequestDispatcher to forward to a JSP page. In my code, I need to be able to handle errors if the page that it tries to forward to a page that doesn't exist in order to avoid seeing:
javax.servlet.ServletException: The page '/myapp/mydir/vehicleChange1.jsp' does not exist.
in the browser window (the path to the page is determined at runtime based on some input parameters). Anyway, for some reason I am having trouble with this. I've tried 2 ways:
1. I've tried catching the ServletException in the servlet. Doesn't work. I can't find any documentation about this, but I'm guessing that by the time the exception is thrown, my servlet has lost control.
2. On Sun's website (http://java.sun.com/docs/books/tutorial/servlets/communication/request-dispatcher.html), they claim that when you do:
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page);
the RequestDispatcher will be null if the page doesn't exist. They even show a specific example where the check for null. I tried that, again, didn't work. In the simplest form:
if (dispatcher==null)
{
System.out.println("dispatcher null");
}
and got nothing.
Ideas, anyone? Thanks in advance!
 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S.- I'm running my code with Jrun 2.3 if it makes any difference.
 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Oh, and BTW, it works just fine in JRun3 (catching the exception- though not the SErvletException, but it does a allaire.jrun.JRunServletException). Unfortunately, we still deploy on 2.3
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic