This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Exception redirect to page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Exception redirect to page" Watch "Exception redirect to page" New topic
Author

Exception redirect to page

Dan Parsons
Ranch Hand

Joined: May 30, 2007
Posts: 70
Sometimes I have Servlets in my Tomcat 4.1.27 container that display information and there might be times when an Exception happens. I like to have a redirect to show a user friendly error page. The below is how I have been doing it and was wondering if this is the proper way to do it where the error message goes to the Tomcat log file and the page redirects.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

It is not the proper way. Rather you should not catch the exception at all. Declare your error page(es) in the web.xml. The Servlet Specificatin spells all of this out in the error handling section.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
In the web.xml


someErrorfullPage.jsp




Thanks,


cmbhatt
Majid Al-Fifi
Ranch Hand

Joined: Aug 22, 2006
Posts: 45
cmbhatt,

In the error page you will not have "code that may cause the exception" rather you may have code that generate useful message to the user about what the problem was.

regards,

Majid


SCJP1.4, SCWCD1.4
Kishore Nallamothu
Greenhorn

Joined: Aug 28, 2006
Posts: 9
Hi

U said <error-page> in web.xml
my Quest. is where we write <error-page> tag i.e.in servlet tag or
servlet-mapping tag.


Thanks in Advance
Vyas
Greenhorn

Joined: May 21, 2007
Posts: 17
Does this mean if it generates an error/exception in the action/service class it will log the error and redirect to the error.jsp?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Vyas",
after multiple requests to correct your display name, I am now disabling your account.

If you would like to participate in this conversation, please do so with an account with a valid display name.

Dave
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
Kishore there is no error tag in servlet or servlet-mapping tag. There is a separate tag called

<error-page> and the attributes of these are

<error-code>
<exception-type>
<location>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception redirect to page
 
Similar Threads
response.sendRedirect() error
Tomcat 5.0.24 can't use error page
java.lang.Throwable in web.xml
Not Redirecting to error page
process errors of user defined URL