1. Need to configure relevant information in Web. xml
[HTML]View Plaincopy print?
- <!--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>
- <!--just stare at commissioning and cannot be commented out during a formal deployment
- <!--This configuration is also possible, indicating that a 500 error occurred, go to 500.jsp page processing. -
- <error-page>
- <error-code>500</error-code>
- <location>/500.html</location>
- </error-page>
- <!--Such a configuration means that if a JSP page or a servlet occurs with an java.lang.Exception type (which of course contains subclasses), the exception goes to 500.jsp page 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>
- <!--
- When both Error-code and Exception-type are configured, the page priority of the Exception-type configuration is high
- And a 500 error occurred, the exception exception will jump to 500.jsp
- -->
<!--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><!--just stare at debugging and cannot be annotated during a formal deployment- ><!--This configuration is also possible, indicating that a 500 error occurred, go to 500.jsp page processing. --><error-page> <error-code>500</error-code> <location>/500.html</location> < /error-page> <!--Such a configuration means that if a JSP page or servlet occurs java.lang.Exception type (of course, it contains subclasses) the exception goes to 500.jsp page 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><!-- When both Error-code and Exception-type are configured, the Exception-type configuration has a high page priority and a 500 error, which jumps to 500.jsp--> when an exception exception occurs;
2. If the configuration is HTML, do not configure it separately
If the configuration is JSP, you need to set Iserrorpage to True,
and <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "iserrorpage=" true "%>
3. Get exception information and output
[HTML]View Plaincopy print?
- <% @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 Server Internal error </title>
- </head>
- <body>
- <div class="Ui-alert-panel">
- <H1> server Internal error </H1>
- <p> An error occurred while processing your request! Please make sure you are working in the right way. </P>
- </div>
- <div style="Display:none;" >
- <%//Here output exception information
- Exception.printstacktrace ();
- Bytearrayoutputstream ostr = new Bytearrayoutputstream ();
- Exception.printstacktrace (New PrintStream (OSTR));
- Out.print (OSTR);
- %>
- </div>
- </body>
- </html>
<% @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" >
Web. XML configuration error page, and output error message