This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to handle a RequestDispatcher if the page doesn't exist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How to handle a RequestDispatcher if the page doesn Watch "How to handle a RequestDispatcher if the page doesn New topic
Author

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

Katie McCann
Ranch Hand

Joined: Jul 24, 2000
Posts: 45

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

Joined: Jul 24, 2000
Posts: 45
P.S.- I'm running my code with Jrun 2.3 if it makes any difference.
Katie McCann
Ranch Hand

Joined: Jul 24, 2000
Posts: 45

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
 
GeeCON Prague 2014
 
subject: How to handle a RequestDispatcher if the page doesn't exist