[Jsp/servlet Entry] JSP Error processor

Source: Internet
Author: User
Tags thread throw exception
When Js|servlet started writing JSP, it was always disturbed by JSP's error messages, without a beginner who knows a little bit about the relationship between JSP and the servlet, all you see is a stack of messages, even exceptions, that include the details of the error messages, and But for beginners it is unfriendly and difficult to read. Basically, as long as you understand the relationship between JSP and the servlet, and understand the Java translation message and exception processing, it is not difficult to understand the error generated by the error report when you write the JSP Web pages.

We know that JSP will eventually be converted to a servlet, while running is really about the servlet, it can happen at two times: a JSP is converted to a servlet's source code, and because of JSP errors, it creates a servlet's original password, Or it has been converted to a servlet code, but the translator detects the error, which is called translation time processing Errors; The second error occurs when the customer asks for a servlet, The exception is due to a mistake that was not considered in the program's logic or operations, which is called the client Request time processing Errors.

How do you find out what bugs happen when you run JSP lines? If the error report shows that "... encountered an internal ...", it could be a swap mistake, or "generated servlet error: [Javac] compiling 1 source File ... ", it's possible that the translator found bugs in the translation of the servlet code, and you have to check to see if the JSP code is wrong, or that the program logic clearly allows the translator to detect bugs.

What is the error that program logic clearly lets the translator detect? A beginner's more frequent but not easily detected error may be "superfluous", for example:

<% @page contenttype= "Charset=big5" errorpage= "errorhandler.jsp"%><% throw new Exception ("This is a false error, just throw out the exception ^ _^ ");%>


This JSP program at first glance is not wrong, we just throw out an exception, but notice that it is at the bottom of the line, and after the conversion to the servlet, this line is actually converted to Out.write (' \ R '), and before that, we throw an exception directly, in other words, Out.write (' \ R '); This line will never be executed, this logic error, the translator can be checked out, so the error page will be back to the "unreachable statement", correct this error, is to the JSP pages in the rest of the broken line.

If the servlet is translated, after the user requests it, the exception is created by errors in the running process, which, if not handled, will ultimately be handled by container, and container the message back to the client, which is basically what happens to the client. Encountered an internal error ... ", you can judge the client Request time processing Errors by making an exception to the stack tracking message on the return page.". ").

You can also customize the processing of an exception, as long as the page instruction element is used, the ErrorPage specifies the error-processing JSP page, and the error-processing JSP page is designed to Iserrorpage true, For example, our demo page errordemo.jsp is:

<% @page contenttype= "Charset=big5" errorpage= "errorhandler.jsp"%><% throw new Exception ("This is a false error, just throw out the exception ^ _^ ");%>


This page is simply throwing out one of our custom exceptions, and notice that we set an exception to the processing page, which is errorhandler.jsp, and its contents are:

<% @page contenttype= "Text/html;charset=big5" iserrorpage= "true"%><% @page import= "Java.io.PrintWriter"% >

The exception object is the hidden object of the JSP, and the exception object that is thrown out by errordemo.jsp is included in the exception, we simply show the message after ToString (), that is <%=exception%> And we can also display the exception stack, Printstacktrace () accepts a PrintWriter object as a reference, so we decorate the Out hidden object (which is JspWriter) as a PrintWriter object, and, This uses the exception Printstacktrace () method to show exception stack tracking.

The results of this example are as follows:




Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.