Js|servlet| page | Auto Jump JSP has a directive that defines the JSP's error page and jumps to the page output error when this JSP page goes wrong
Log. Examples of the following:
<%@ page errorpage= "errorpage.jsp"%>
The errorpage.jsp code looks like this:
.....
<%@ page iserrorpage= "true"%>
....
Output error Log
<%= exception.getmessage ()%>
However, this can only be controlled at the JSP page level. In the Java EE implementation, a lot of the situation is that JSP is often only as a page display
, the operations related to the database operations are in the background of the servlet execution, processing finished and then jump a JSP display
Page. This setup framework realizes the MVC structure, which makes the maintenance of the whole system much less difficult.
In the actual work, although the system adopted the above setup framework, but by the Java EE Developer level and programming of the acquisition
It's always possible to get out of the box and often encounter a developer in a servlet who is improperly handling a possible error. Most often
In one case, catch a exception e, and then just call E.printstacktrace (). This causes
The consequences are very serious. Once a exception occurs, the Web page appears white screen, we can see from two aspects:
(1) If it is used by the user. He is often overwhelmed, and may not yet know that there is a mistake, continue to use
System, but this time the system has been wrong, on the wrong basis to continue the business process, often resulting in more system-level
Other mistakes.
(2) If it is a developer. The user reflects this error, but does not know the cause of the error. Can only say appear to screen.
Developers need to debug the wrong words, not to get the application server input to see the error log, locate the possible error of the original
Because, and then make the wrong arrangement.
Here, I introduce an effective servlet error-handling mechanism that will all exception error content
Throw to the Web page, let the user have an immediate error, and can be timely to submit the error content to the developer set
The reason for the bit error.
In fact, this mechanism is very simple. The idea is to define an abstract Baseservlet base class, which inherits HttpServlet.
and add an abstract of the abstraction public void Doworkflow (httpservletrequest request,httpservletresponse response)
method, which is a method that all baseservlet subclasses must and need to implement only. Baseservlet base class of course to implement service method-public final void service (HttpServletRequest request,httpservletresponse response)
。 Its code snippet is as follows:
Public final void Service (HttpServletRequest request,httpservletresponse response)
Throws Servletexception, IOException {
try{
Before executing doworkflow (), you can handle issues such as whether you have permission to handle
.......
Doworkflow ();
}catch (Exception e) {
StringWriter out = new StringWriter ();
E.printstacktrace (new PrintWriter (out));
Request.setattribute ("Err_msg", out.tostring ());
RequestDispatcher rd = This.getservletcontext (). Getrequestdispatcher ("errorservlet.jsp");
Rd.forward (Request,response);
}
}
Errorservlet.jsp is very simple. The code snippet is as follows:
....
Output error Log
<%= Request.getattribute ("err_msg")%>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.