Jsp (the preferred choice for SUN Enterprise Applications) has a command that defines the error page of jsp (the preferred choice for SUN Enterprise applications), and here jsp (the preferred choice for SUN Enterprise applications) an error occurred while redirecting to the page when a page error occurred.
Logs. Example:
<% @ Page errorPage = "errorPage. jsp (preferred for SUN Enterprise Applications)" %>
The code for errorPage. jsp (preferred for SUN Enterprise Applications) is as follows:
.....
<% @ Page isErrorPage = "true" %>
....
// Output Error Log
<% = Exception. getMessage () %>
However, this can only be controlled at the page layer of jsp (the preferred choice for SUN Enterprise applications. In the implementation of J2EE, jsp (the first choice for SUN enterprise-level applications) is often used only as page display.
Show, business-related database operations are performed in the backend servlet, after processing is completed, jump to a jsp (preferred for SUN Enterprise Applications) display
Page. This setting framework implements the MVC structure, greatly reducing the maintenance difficulty of the entire system.
In practice, although the system uses the above setup framework
Normally, developers may encounter improper handling of errors in servlet. Most common
In one case, catch an Exception e and then only call e. printStackTrace (). This causes
The consequences are very serious. Once an Exception occurs, a white screen will appear on the web page. We can look at the following two aspects:
(1) if it is a user. He is often at a loss, and may not know that there are errors, continue to use
System, but at this time the system has gone wrong, continue the business process on the basis of the error, often cause more system level
Other errors.
(2) developers. The user reflects the error, but does not know the cause of the error. Only white screen is required.
If a developer needs to debug and catch an error, check the error log at the input end of the application server to locate the source
And then troubleshooting.
Here, I will introduce an effective servlet error handling mechanism, which will handle all exceptions
The error is thrown to the web page so that the user can immediately report the error to the developer.
Bit Error cause.
In fact, this mechanism is very simple. The idea is to define an abstract BaseServlet base class that inherits HttpServlet.
Add an abstract public void doWorkFlow (HttpServletRequest request, HttpServletResponse response)
This method is a required and only required method for all BaseServlet subclasses. The BaseServlet base class must of course implement the service method -- public final void service (HttpServletRequest request, HttpServletResponse response)
. Its code snippets are 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 the permission to handle them.
.......
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 (preferred for SUN Enterprise Applications )");
Rd. forward (request, response );