GeeCON Prague 2014*
The moose likes Tomcat and the fly likes How to customize the 404 error page for Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Tomcat
Bookmark "How to customize the 404 error page for Tomcat" Watch "How to customize the 404 error page for Tomcat" New topic
Author

How to customize the 404 error page for Tomcat

William Chang
Greenhorn

Joined: Aug 06, 2003
Posts: 4
I am trying to customize 404 error page for Tomcat server. Sometimes, this is very important because you may need to redirect the specific request to special error page. Sure, you can add the 404 error page in your web.xml, however, this is not customized page because it doesn't know what is the original request. I know, you can customize a 404 error page for Apache server by knowing what are the original requests, like it was mentioned in: Custom Error Pages with PHP and Apache. My question is that if you cann't configure your Apache server but Tomcat, what I can do to customize the 404 error pages? Thanks
[ August 06, 2003: Message edited by: William Chang ]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Moving to the Tomcat forum.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

you can add the 404 error page in your web.xml, however, this is not customized page because it doesn't know what is the original request.

Sure it does. Since the error page is forwarded to from the request that causes the error, the request environment is still available. In fact, a number of request attributes are added to the request to help you create helpful customized error messages.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

By the way, the relevant section of the Java Servlet 2.3 Specification is SRV.9.9.
And here's some not-so-subtle advice: if you are working with Tomcat or any other servlet engine and haven't read the Servlet Specification, you're doing it wrong!
Seriously, the spec may seem daunting but it's actually very well written, especially for a technical specification, and is an easy read. Do yourself a favor and get a copy from Sun's web site.
Keep the PDF handy, or even make a hard-copy for your desk.
hth,
bear
[ August 06, 2003: Message edited by: Bear Bibeault ]
William Chang
Greenhorn

Joined: Aug 06, 2003
Posts: 4
I wish that you have tried it by youself. Thanks.
William Chang
Greenhorn

Joined: Aug 06, 2003
Posts: 4
I think that I have not made myself clear. In your web.xml, you add:
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
Whenever there is 404 error, e.g., the user type XXX.com/wrong_url, the tomcat will lead to /error.jsp. The error.jsp won't know where the 404 error comes from, I mean, because the user typed wrong_url. If you know, please let me know. Thanks.
[ August 09, 2003: Message edited by: William Chang ]
[ August 09, 2003: Message edited by: William Chang ]
 
GeeCON Prague 2014
 
subject: How to customize the 404 error page for Tomcat