Custom Error Page in Tomcat 7 for Error Code 500
Asked Answered
E

2

16

Guys I am struggling with the problem of opening my custom error page in Tomcat in Windows Environment. Some of solution I got but no luck. I tried almost all links but its not working for me. Some of them working for other solution deployed in Tomcat but not for my Web Service

My web.xml

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<error-page>
    <location>/error.html</location>
</error-page>


I am sending error from my Servlet 3.0 Application response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

I have put my error.html in the root directory of my WebService.

Some of the links I got for JSP page instead of HTML that also I have tried

In case of JSP I used:

<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Error</h1>
</body>
</html>


FYI it is working fine for other Tomcat inbuilt solution it's working perfectly. But in my Webservice I am getting response 500 but page is opening blank. And one other thing I have disabled my Internet Explorer 9.0 show error friendly page still response coming 500 from servlet. But error page is coming empty.

If any idea or any doubt about my query just let me know. I need it ASAP.

Execratory answered 13/4, 2013 at 11:19 Comment(5)
Why is a web service trying to serve HTML pages when an error occurs ?If
...what does your servlet and JSPs have to do with web services? are you sure you know what a web service is?Axenic
because we are using our custom error page in our projectExecratory
yeah i know what webservice do servlet i am using to send all output to browser for download image and jsp page/html page i want to show my custom error page clear?Execratory
@SumitSharma I am experiencing a similar problem to the one you described - Tomcat is printing a different error page than the one I specified in web.xml. Did you solve the problem?Ortrude
V
30

Try putting the following snippet in your WEB-INF/web.xml

<error-page>
    <error-code>500</error-code>
    <location>/Error.jsp</location>
</error-page>

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/Error.jsp</location>
</error-page>

In this case Error.jsp is at the same level as WEB-INF directory (not inside it).

Vimen answered 13/4, 2013 at 14:55 Comment(1)
this seems to have been helpful for me! of note, the ordering is important, you want the 500 mapping before the Exception mapping (as is shown)Contaminant
W
7

The same can be implemented programmatically if you use embedded Tomcat for example like this:

        Context appContext = ...            
        ErrorPage errorPage = new ErrorPage();
        errorPage.setErrorCode(HttpServletResponse.SC_NOT_FOUND);
        errorPage.setLocation("/my_custom_error_page.html");
        appContext.addErrorPage(er);
Waddell answered 15/2, 2017 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.