Configure 404 custom error page details in Tomcat

  • 2020-06-12 11:09:27
  • OfStack

In order to get a good user experience, users should not be exposed to the 404 page. The starting point of the problem is that I define the error page in Struts2, which is defined as follows in Struts2:


 <default-action-ref name="pagenotfound"></default-action-ref>   
 <action name="pagenotfound"> 
       <result>/pagenotfound.html</result> 
 </action> 

This means that when I visit action, if I don't find action, I go to this page, but if I don't use.do or.action and use.jsp or.html to access the page,struts will not handle it. The result is that the 404 error still occurs.
It is no longer the processing scope of struts, so this should be the processing scope of the application. After verification, the custom error page can be set in web.xml of the project, and the Settings are as follows:


<error-page> 
    <error-code>404</error-code> 
    <location>/pagenotfound.html</location> 
</error-page> 

Now visit one of the pages under the project that does not exist, and you will go to the custom pagenotfound page, so that the ES23en-ES24en-ES25en configuration in struts can be removed, because the 404 is left to tomcat.

Then, I type in http://localhost/asdfasdfafd1 which does not exist, 404 still appears,
Back to 1, our web.xml is under an application, it should be 404 for this application, and http://localhost/ is accessing tomcat's own application, then the ES38en.xml configuration should be configured under webapp/Root.

Below the Root directory are the Tomcat apps, just replace them with your own.
Now enter an address that does not exist and successfully jump to the custom error page.

Tomcat 404/500 error, custom error page

You want to be able to give a user friendly, realistic interface when there are 404, 500 errors on the server
Just add 1 configuration to the web.xml of the project


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


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

This will automatically display the page you just specified when you visit a page that doesn't exist


<!-- 400 error  -->
<error-page>
<error-code>400</error-code>
<location>/error.jsp</location>
</error-page>
<!-- 404  There are no errors on the page  -->
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<!-- 500  Server internal error  -->
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<!-- java.lang.Exception Exception error , Multiple similar error messages can be defined based on this flag  -->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
<!-- java.lang.NullPointerException Exception error , Multiple similar error messages can be defined based on this flag  -->
<error-page>
<exception-type>java.lang.NullPointerException </exception-type>
<location>/error.jsp</location>
</error-page>

<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/error.jsp</location>
</error-page>

The details are as follows:
Tomcat error page is org. apache. catalina. valves. ErrorReportValve class output. If you want to customize the error page, you do not need to modify the class. The Servlet specification declares the relevant API as defined in web.xml for each web application. Can be configured by error type, error code. Such as:


<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 begin with "/" so that any path 404 error page and exception error will map to both files. Then place two files, 404.jsp and exception.jsp, under the errorpages referenced in this web.
Error page 404.jsp:


<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<html>
<header>
<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 request are printed in the code. You can also see which file was visited incorrectly and which error page was jumped to for more detailed and human error handling. For example, suggest possible correct urls and so on.
For example: visit a page that does not exist page_ES92en_exist.html, and 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. Firefox displays normally. You can add some additional information to increase the page size to more than 512 bytes. If it still does not display, check the IE setting to check that option.

Exception handling page ES120en. jsp:


<%@ page contentType="text/html; charset=UTF-8" isErrorPage="true" %>
<%@ page import="java.io.*" %>
<html>
<header>
<title>exception page</title>
<body>
<hr/>
<pre>
<%
response.getWriter().println("Exception: " + exception); 

if(exception != null)
{
  response.getWriter().println("<pre>"); 
  exception.printStackTrace(response.getWriter()); 
  response.getWriter().println("</pre>"); 
}

response.getWriter().println("<hr/>"); 
%>

Note that isErrorPage familiarity must be true to use the exception object. exception is the caught exception. exception can be handled here, such as logging, redirecting, and so on. exception trace is printed here.
Error pages like 500, 505, etc. are handled similarly to 404.


Related articles: