Custom 404 error page

 This is a detail that should be implemented in order to make the program more friendly. The configuration is very simple, configure the following information in the web.xml file of the project:

<error-page>
  <error-code>404</error-code>
  <!--Specify the 404.jsp page file in the WebContent root directory-->
  <location>/404.jsp</location>
</error-page>

 The directory results are as follows:

The struts2 configuration file struts.xml does not require any configuration.

However, in actual operation, the operation interface appears as shown in the following figure:

 This phenomenon indicates that a 404 error has indeed occurred, but it has not been assigned to the 404 page defined by itself. Why? Is the path wrong? Double check that the path is correct. It can be solved by setting the following settings in "Tools->Internet Options->Advanced" of IE browser:

 uncheck the "Show friendly http error message" to see the 404 error page defined by yourself, as follows:

 
 web.xml Configuration additions:

    <!-- Default error handling page -->  
    <error-page>  
        <error-code>403</error-code>  
        <location>/403.html</location>  
    </error-page>  
    <error-page>  
        <error-code>404</error-code>  
        <location>/404.html</location>  
    </error-page>  
    <!-- Only watch out when debugging, can't comment when formally deploying -->  
    <!-- This configuration is also possible, indicating that when a 500 error occurs, go to the 500.jsp page for processing. -->  
    <error-page>   
        <error-code>500</error-code>   
        <location>/500.html</location>   
    </error-page>   
      
    <!-- This configuration means that if an exception of the java.lang.Exception type (including subclasses of course) occurs in the jsp page or servlet, it will go to the 500.jsp page for processing. -->  
    <error-page>   
        <exception-type>java.lang.Exception</exception-type>   
        <location>/500.jsp</location>   
    </error-page>   
      
    <error-page>   
        <exception-type>java.lang.Throwable</exception-type>   
        <location>/500.jsp</location>   
    </error-page>  
    <!--   
    Important note: When both error-code and exception-type are configured, the page configured by exception-type has higher priority  
    That is, a 500 error occurs, and when an Exception occurs, it will jump to 500.jsp  
     -->

If html is configured, no additional configuration is required; if Jsp is configured, isErrorPage needs to be set to true,
ie <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding ="UTF-8" isErrorPage="true"%> .

The jsp page is as follows:

<%@page import="java.io.PrintStream"%>  
<%@page import="java.io.ByteArrayOutputStream"%>  
<%@ include file="WEB-INF/views/includes/tags.jsp"%>  
<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8" isErrorPage="true"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>500 Internal Server Error</title>  
</head>  
<body>  
    <div class="ui-alert-panel">  
        <h1>Internal server error</h1>  
        <p>An error occurred while processing your request! Please make sure you are operating in the correct way. </p>  
    </div>  
    <div style="display:none;">  
      <% //Output exception information here  
        exception.printStackTrace();  
  
        ByteArrayOutputStream ostr = new ByteArrayOutputStream();  
        exception.printStackTrace(new PrintStream(ostr));  
        out.print (ostr);  
      %>  
    </div>  
</body>  
</html>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326111596&siteId=291194637