Tomcat common error jumps to the specified page

Source: Internet
Author: User
Tags error handling exception handling

Through the URL to access the resources in Tomcat, there are many common mistakes, such as 404 Ah, 500 ah what, if not set, browser page will directly display these errors, users feel of course is quite bad, then how to solve this problem.

It's really simple, just add some configuration to the project's web.xml.

<error-page>
    <error-code>400</error-code>
    <location>/web-inf/views/error/400. jsp</location>
</error-page>
<error-page>
    <error-code>403</error-code >
    <location>/WEB-INF/views/error/403.jsp</location>
</error-page>
< error-page>
    <error-code>404</error-code>
    <location>/web-inf/views/error/404.jsp </location>
</error-page>
<error-page>
    <error-code>500</error-code >
    <location>/WEB-INF/views/error/500.jsp</location>
</error-page>
<!--java.lang.Exception exception error, according to this tag can define multiple similar error-->
<!--<error-page>-->
   <!--< Exception-type>java.lang.exception</exception-type>-->
   <!--<location>/error.jsp</ Location>-->
<!--</error-page>-->



The details are as follows:

Tomcat's error page is output from the Org.apache.catalina.valves.ErrorReportValve class. If you want to customize the error page, you do not need to modify the class. The Servlet specification declares the relevant APIs, and only needs to be defined in the web.xml of each Web application. can be configured by error type, error code. For example:

<web-app xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version= "2.5" >

<display-name>welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

<error-page>
<error-code>404</error-code>
<location>/errorpages/404.jsp</location>
</error-page>

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

</web-app>

Note that the error page must start with "/" so that any path's 404 error pages and exception errors will be mapped to these two files. Then, under the errorpages of this Web reference, place 404.jsp, exception.jsp two files.

Error page 404.jsp:

<%@ page contenttype= "text/html; Charset=utf-8 "%>
<%@ page import= "java.io.*"%>
<%@ page import= "java.util.*"%>
<title>404 page</title>
<body>

<pre>
<%
enumeration<string> attributenames = Request.getattributenames ();
while (Attributenames.hasmoreelements ())
{
String AttributeName = Attributenames.nextelement ();
Object attribute = Request.getattribute (attributename);
Out.println ("request.attribute['" + AttributeName + "'] =" + attribute);
}
%>
</pre>

All the variables in the request are exported in the code. You can also see which file is being accessed and which error page to go to for more detailed, more humane error handling. For example, prompt for a possible correct URL, and so on.

For example, to access a non-existent page page_not_exist.html, the information displayed is:

request.attribute[' Javax.servlet.forward.request_uri '] =/page_not_exists.html
request.attribute[' javax.servlet.forward.context_path '] =
request.attribute[' javax.servlet.forward.servlet_path '] =/page_not_exists.html
request.attribute[' javax.servlet.forward.path_info '] =/errorpages/404.jsp
request.attribute[' javax.servlet.error.message '] =/page_not_exists.html
request.attribute[' javax.servlet.error.status_code '] = 404
request.attribute[' javax.servlet.error.servlet_name ' = default
request.attribute[' Javax.servlet.error.request_uri '] =/page_not_exists.html

Note that the error page must be greater than 512 bytes, or IE will not be displayed. Because IE only displays error pages larger than 512 bytes by default. The normal display in Firefox. You can add additional information to expand the page size to more than 512 bytes. If it still cannot be displayed, check the IE settings to select it.


Exception Handling Page exception.jsp:

<%@ page contenttype= "text/html; Charset=utf-8 "iserrorpage=" true "%>
<%@ page import= "java.io.*"%>
<title>exception page</title>
<body>
<pre>
<%
Response.getwriter (). println ("Exception:" + Exception);

if (Exception!= null)
{
Response.getwriter (). println ("<pre>");
Exception.printstacktrace (Response.getwriter ());
Response.getwriter (). println ("</pre>");
}

Response.getwriter (). println ("%>

Note Iserrorpage must be true to use the exception object. Exception is the exception that is caught. Exception can be processed here, such as logging, redirection, and so on. The exception trace is printed here.

500, 505 error page processing is similar to 404.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.