Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automatically jump to error page in JSP Servlet

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to automatically jump to the error page in JSP Servlet. I hope you will get something after reading this article. Let's discuss it together.

To implement this function in a JSP Servlet development example, you can use an instruction of JSP, which can define the error page of JSP and jump to the page output error when this JSP page goes wrong.

Journal. Examples are as follows:

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

However, this can only be controlled at the JSP page level. In the implementation of J2EE, in many cases, JSP is often displayed only as a page, and the database operations related to the business are performed in the background servlet, and then jump to a JSP display page after processing. This setting framework realizes the MVC structure, which greatly reduces the maintenance difficulty of the whole system.

In the actual work, although the system uses the above setup framework, the level of J2EE developers and programming habits may not understand, and developers often encounter inappropriate handling of possible errors in servlet. One of the most common cases is to catch an Exception e and then just call e.printStackTrace (). The consequences are very serious. Once Exception occurs, the web page will show a blank screen. We can look at it from two aspects:

◆ if it is a user. He is often at a loss, and may not know that an error has occurred and continue to use the system, but at this time the system has already made a mistake, and continuing the business process on the basis of the error will often lead to more system-level errors.

If ◆ is a developer. The user reflects the error but does not know the cause of the error. It can only be said that the screen is blank when it appears. If the developer needs to debug and catch the error, he must get the input of the application server to view the error log, locate the possible cause of the error, and then troubleshoot it.

Here, I introduce an effective servlet error handling mechanism, which throws all the Exception error content to the web page, so that the user has an error immediately, and can submit the error content to the developer in time to locate the cause of the error.

In fact, this mechanism is very simple. The idea is to define an abstract BaseServlet base class that inherits HttpServlet. And add an abstract abstract public void doWorkFlow (HttpServletRequest request,HttpServletResponse response) method that all BaseServlet subclasses must and only need to implement. Of course, the BaseServlet base class implements the 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 deal with questions such as whether you have permission to handle it or not. 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")% > after reading this article, I believe you have a certain understanding of "how to automatically jump to the error page in JSP Servlet". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report