I have been using tomcat, and today I thought of a question: Customizing the 404 error page. In order to get a good user experience, pages like 404 should not be exposed to users. The starting point of the problem is that I define the error page in Struts2, which is defined in Struts2:
<default-action-ref name="pagenotfound"></default-action-ref> <action name="pagenotfound"> <result>/</result> </action>
This means that when accessing the action is. If the action is not found, access this page, but if I do not use the .do or .action style, but directly use .jsp or .html to access the page, struts will not be processed. The result is that a 404 error still appears.
Now it is no longer the processing scope of struts, so this should be the processing scope of the application. After verification, you can set a custom error page in the project, and set it as follows:
<error-page> <error-code>404</error-code> <location>/</location> </error-page>
Now visit a non-existent page below the project and will jump to the custom pagenotfound page. In this way, the default-action-ref configuration in struts can be removed. Because 404 is handed over tomcat for processing.
Then, I enter http://localhost/asdfasdfafd an address that does not exist, and the result is 404 still appears.
Think back, we just now were under a certain application, and what we should be dealing with should be the 404 of this application, and http://localhost/ access to tomcat's own application, so this configuration should be configured in the application under webapp/Root/.
The Tomcat application is placed under the Root directory, just replace it with your own.
So now enter the non-existent address and successfully jump to the customized error page.
404/500 error in Tomcat, custom error page
When the server errors are 404 or 500, I hope to give a user-friendly real interface
Just add some configuration to the project
<error-page> <error-code>404</error-code> <location>/</location> </error-page> <error-page> <error-code>500</error-code> <location>/</location> </error-page>
In this way, when you access the page that does not exist, the page you just specified will be automatically displayed.
<!-- 400mistake --> <error-page> <error-code>400</error-code> <location>/</location> </error-page> <!-- 404 页面不存在mistake --> <error-page> <error-code>404</error-code> <location>/</location> </error-page> <!-- 500 服务器内部mistake --> <error-page> <error-code>500</error-code> <location>/</location> </error-page> <!-- 异常mistake,依据这个标记可定义多个类似mistake提示 --> <error-page> <exception-type></exception-type> <location>/</location> </error-page> <!-- 异常mistake,依据这个标记可定义多个类似mistake提示 --> <error-page> <exception-type> </exception-type> <location>/</location> </error-page> <error-page> <exception-type></exception-type> <location>/</location> </error-page>
The specific ones are as follows:
The error page of Tomcat is output from the 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 each web application. It can be configured according to the error type and error code. For example:
<web-app xmlns="/xml/ns/javaee" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/javaee /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/</location> </error-page> <error-page> <exception-type></exception-type> <location>/errorpages/</location> </error-page> </web-app>
Note that the error page must start with "/", so that any path 404 error page and exception error will be mapped to these two files. Then place two files under the errorpages referenced by this web.
Error page:
<%@ page contentType="text/html; charset=UTF-8" %> <%@ page import=".*" %> <%@ page import=".*" %> <html> <header> <title>404 page</title> <body> <pre> <% Enumeration<String> attributeNames = (); while (()) { String attributeName = (); Object attribute = (attributeName); ("['" + attributeName + "'] = " + attribute); } %> </pre>
The code outputs all variables in the request. From this, you can also see which file is accessed and which error page has been skipped to, so as to perform more detailed and user-friendly error handling. For example, prompt for possible correct URLs, etc.
For example: Visit a page that does not exist page_not_exist.html, and the information displayed is:
['.request_uri'] = /page_not_exists.html
['.context_path'] =
['.servlet_path'] = /page_not_exists.html
['.path_info'] = /errorpages/
[''] = /page_not_exists.html
['.status_code'] = 404
['.servlet_name'] = default
['.request_uri'] = /page_not_exists.html
Note that the error page must be greater than 512 bytes, otherwise IE will not display it. Because IE only displays error pages larger than 512 bytes by default. It is displayed normally in Firefox. Some additional information can be added to expand the page size to more than 512 bytes. If it still cannot be displayed, please check the IE settings and select the option.
Exception handling page:
<%@ page contentType="text/html; charset=UTF-8" isErrorPage="true" %> <%@ page import=".*" %> <html> <header> <title>exception page</title> <body> <hr/> <pre> <% ().println("Exception: " + exception); if(exception != null) { ().println("<pre>"); (()); ().println("</pre>"); } ().println("<hr/>"); %>
Note that isErrorPage familiarity must be true to use exception object. exception is the caught exception. Exceptions can be processed here, such as logging, redirecting, etc. Here is the exception trace.
The processing of error pages such as 500, 505 is similar to 404.