File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet Error Page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Error Page" Watch "Servlet Error Page" New topic

Servlet Error Page

Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
Hi there,

As a result of this wonderful Head First JSP & Servlets book, I can configure error.jsp pages for my application and for individual jsp's. However, I would like to know if there's a way to configure an error jsp page for a servlet as well.

I have a servlet gets info from a db and forwards to a jsp for the view. But sometimes the code in the servlet throws an exception, and I want to direct users to a specific error page rather than an application catch-all. How should I do that?


Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63857

Declare a general error page or pages (you can declare different pages for different types of errors) in the web.xml file. This will handle all errors not otherwise handled so that you do not need to declare anything for each indovidual JSP or servlet.

[Asking smart questions] [About Bear] [Books by Bear]
Tom McAmmond
Ranch Hand

Joined: Feb 16, 2004
Posts: 58
Ah... but I *want* to have a special error page for a couple of my servlets. Say I have 100 servlets, but 5 of them need an error page that's different than the pages for the other 95 servlets. Can I do that?
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

In your servlets, you can catch an exception and forward to any page you like.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Servlet Error Page
It's not a secret anymore!