A server 500 exception occurs and, if handled by default, jumps to the Tomcat default exception page after the exception is captured, as shown in the following illustration.
Regardless of which Web site is the same, Tomcat also allows custom styling to meet the needs of customization. That is, the configuration in the Web.xml file:
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location >
</error-page>
First, the logic of the band. If an error occurs in the execution of a JSP page, the JSP engine automatically generates an exception object, and if the JSP page specifies another JSP page as the error handler, the JSP engine puts the exception object into the request object and uploads it to the error handler. If you have the impression of writing a Servlet, this is and the change to the template JSP Javax.servlet.forward.request_uri a way of thinking, keep the original requested path rather than the JSP page that path. In the error handler, because the value of the Iserrorpage property of the page compilation directive is set to true, the JSP engine automatically declares a exception object, which is obtained from the HTTP parameters contained in the Request object.
The Request object contains very rich exception information, as follows:
You can get it using Java statement request.getattribute ("Javax.servlet.error.status_code"), or you can get it through an EL expression in a JSP page, such as ${requestscope[" Javax.servlet.error.status_code "]}.
This custom error page, though simple, JSP itself also has a very good packaging results, I have seen a lot of other people's resources, but under scrutiny there are also a lot of "learning", so I want to "grind this Wheel"--first location is a JSP page, can also be a servlet, but in case The servlet may not start up, so just use a simple JSP page. We use the JSP page to define the inner class of the method to achieve the separation of the page and logic (no need to write a servlet). The rest of the idea is as follows:
Inside the JSP completes ErrorHandler class, another page calls this ErrorHandler class
Not only can you accept JSP page errors, but also accept the servlet's controller pass errors, and extract as much information as possible
All content is first written to memory, then output from two output streams to the page and file respectively
When you output the error message to the Web page, simply add a few words, you can also write a copy of the information on the Web page to the database or text
can return to Html/json/xml
The implementation code is as follows:
/** * Exception Handling class/class ErrorHandler {//all content first written to memory, and then output from two output streams to the page and file private Bytearrayoutputstream Bytearrayout
Putstream = new Bytearrayoutputstream ();
Private PrintStream PrintStream = new PrintStream (bytearrayoutputstream); /** * Collect error messages * @param request * @param exception * @param out/Public ErrorHandler (httpservletreq
Uest request, Throwable exception, jspwriter out) {setrequest (request);
SetException (Exception);
if (out!= null) {try {out.print (bytearrayoutputstream);//Output to Web page} catch (IOException e) {
E.printstacktrace ();
} log (request);
if (bytearrayoutputstream!= null) try {bytearrayoutputstream.close ();
catch (IOException e) {e.printstacktrace ();
} if (PrintStream!= null) printstream.close ();
/** * * @param request * * private void Setrequest (HttpServletRequest request) {Printstream.println ();
Printstream.println ("User account:" + request.getsession (). getattribute ("UserName"));
PRINTSTREAM.PRINTLN ("Access path:" + getInfo (Request, "Javax.servlet.forward.request_uri", String.class));
PRINTSTREAM.PRINTLN ("error page Address:" + getInfo (Request, "Javax.servlet.error.request_uri", String.class));
PRINTSTREAM.PRINTLN ("Error code:" + getInfo (Request, "Javax.servlet.error.status_code", Int.class));
Printstream.println ("Type of Exception:" + getInfo (Request, "Javax.servlet.error.exception_type", Class.class));
PRINTSTREAM.PRINTLN ("Exceptional Information:" + getInfo (Request, "Javax.servlet.error.message", String.class));
Printstream.println ("Exception servlet:" + getInfo (Request, "Javax.servlet.error.servlet_name", String.class));
Printstream.println ();
The other two objects getInfo (request, "Javax.servlet.jspException", Throwable.class);
GetInfo (Request, "Javax.servlet.forward.jspException", Throwable.class); map<string, string[]> map = Request.getparameteRMap ();
For (String Key:map.keySet ()) {printstream.println ("Parameter in Request includes:");
PRINTSTREAM.PRINTLN (key + "=" + request.getparameter (key));
Printstream.println (); For (Cookie cookie:request.getCookies ()) {//Cookie.getvalue () printstream.println () The cookie in the request includes: "
);
Printstream.println (Cookie.getname () + "=" + Cookie.getvalue ());
Printstream.println (); }/** * * @param exception/private void SetException (Throwable exception) {if (Excepti
On!= null) {PRINTSTREAM.PRINTLN ("exception information");
Printstream.println (Exception.getclass () + ":" + exception.getmessage ());
Printstream.println ();
PRINTSTREAM.PRINTLN ("stack Information");
Exception.printstacktrace (PrintStream);
Printstream.println (); }/** * * @param request */private void log (HttpServletRequest request) {File dir = new File (Request.getsession (). GetSErvletcontext (). Getrealpath ("/errorlog"));
if (!dir.exists ()) {Dir.mkdir ();
String TimeStamp = new Java.text.SimpleDateFormat ("Yyyymmddhhmmsss"). Format (new Date ());
File File = new file (Dir.getabsolutepath () + File.separatorchar + "error-" + TimeStamp + ". txt");
Try (fileoutputstream FileOutputStream = new FileOutputStream (file);
PrintStream PS = new PrintStream (FileOutputStream)) {//write to File//Ps.print (Bytearrayoutputstream);
catch (FileNotFoundException e) {//E.printstacktrace ();
catch (IOException e) {//E.printstacktrace ();
catch (Exception e) {//E.printstacktrace (); }/** * * @param request * @param key * @param type * @return * /@SuppressWarnings ("unchecked") Private <T> T GetInfo (httpservletrequest request, String key, class<t& Gt Type) {Object obj = Request.getattribute (key); return obj = null?
Null: (T) obj;
}
}
This will allow you to complete the control of the exception. The following defines web.xml, so that Tomcat error leads to the page we just specified error.jsp
<!--404 page does not exist error-->
<error-page>
<error-code>404</error-code>
<location >/WEB-INF/jsp/common/default/error.jsp</location>
</error-page>
<!--//-->
<!--500 server internal error-->
<error-page>
<error-code>500</error-code>
<location >/WEB-INF/jsp/common/default/error.jsp</location>
</error-page>
<!--//-->
We arrange a default page with the following
The source code is as follows:
<% @page pageencoding= "UTF-8" iserrorpage= "true"%> <%@ include file= "/web-inf/jsp/common/classicjsp/" util.jsp "%> <! DOCTYPE html>
The above is the entire content of this article, I hope to help you learn.