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

Servlet Error Page

 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks!

Tom
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64828
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your servlets, you can catch an exception and forward to any page you like.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic