Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error Page in Tomcat 7 for Error Code 500

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.

like image 582
Sumit Sharma Avatar asked Apr 13 '13 11:04

Sumit Sharma


People also ask

How do I get Tomcat error page?

If you want it to make generic, I suggest you to define the errors in TOMCAT_HOME/conf/web. xml . It will override default error pages that come with Tomcat .

How do I create a custom error page for when Tomcat is down for maintenance?

A common requirement is to have Apache display a custom maintenance error page when Tomcat is down or when an application running in Tomcat is down. This is quite easy to do. All we need to do is add an ErrorDocument entry to our http. conf file.

What is HTTP Status 500 error in Tomcat?

HTTP Status 500 – Internal Server Error Description The server encountered an unexpected condition that prevented it from fulfilling the request.


2 Answers

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).

like image 101
Harshal Waghmare Avatar answered Oct 05 '22 15:10

Harshal Waghmare


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);
like image 7
Kaira Avatar answered Oct 05 '22 15:10

Kaira