Monday, October 11, 2010

JSP Servlet automatically jump to the wrong instance of the page



Development in JSP Servlet instance to use JSP to achieve this functionality as a command, which can define JSP error page and error in the JSP page Jump to the page when the output error

Log. The following example:

<% @ Page errorPage = "errorPage.jsp"%> errorPage.jsp of the code is as follows: ..... <% @ page isErrorPage = "true"%> .... / / output error log < % = exception.getMessage ()%>

However, this can only be controlled in the JSP page layer. In the J2EE implementation, a lot is often only as a JSP page displays, and business-related database operations are in the background of the servlet execution, and then disposed of after the jump shows a JSP page. This set the framework to achieve the MVC structure, making more difficult the maintenance of the system greatly reduced.

The actual work, though the system settings using the above framework, but by the level of J2EE developers, and custom programming are likely blocked, often encounter developers servlet error on the local treatment may be inappropriate. The most common one is, catch an Exception e, and then just call e.printStackTrace (). This is very serious consequences. Exception occurred once, web page will appear black and white, we are two aspects to it:

鈼?If you are using users. He often overwhelmed and may not know that there is an error, continue to use the system, but this time the system is wrong, wrong to build on the business processes, often resulting in more system-level error.

鈼?If the developer. Users to reflect this error, but do not know the cause of errors. Can only say that there should be black and white. Developers need to debug grasping wrong, to get to the input of the application server error log view, location may be cause of the error, further troubleshooting.

Here, I introduce an effective servlet error handling mechanism, which the contents of all the error Exception thrown to the web page, so that a user error occurs immediately, and the error in time to the developers to submit their positioning error reasons.

In fact, this mechanism is very simple. Idea is to define an abstract BaseServlet base class that inherits from HttpServlet. And add an abstract of the abstract public void doWorkFlow (HttpServletRequest request, HttpServletResponse response) method, this method is that all BaseServlet subclass must be implemented and the only way. BaseServlet course to achieve the service base class method - public final void service (HttpServletRequest request, HttpServletResponse response). It is the code snippet is as follows:

public final void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException (try (/ / in the implementation of doWorkFlow () before you can handle this deal with such issues as whether the permission ....... 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 very simple. Code snippet is as follows:

.... / / Output error log <% = request.getAttribute ("err_msg")%>

So the above is an instance of JSP Servlet development features automatic jump through practice effective?







相关链接:



3GP To MOV



FLV To M4V



sql server mfc dao classes and the mfc Odbc



SWF to MPEG4



No comments:

Post a Comment