The execution process of JSP and its exception handling mechanism

Source: Internet
Author: User

1, the implementation of the JSP process

Although JSP feels like a normal HTML page, it is actually run in the form of a servlet.    Because the JSP file in the first run will be interpreted as a servlet source files, and then compiled into a servlet class file, the last to be run by the servlet container, JSP execution process can be divided into the following: 1) The client made the request; 2) The Web container translates the JSP into the servlet source code, 3) the Web container compiles the generated source, 4) The Web container loads the compiled code and executes; 5) responds to the client with the execution result. 2. The exception handling mechanism of JSPIn addition to the HTML code in the JSP, the other is the Java Script element, so in the JSP page to use Try-catch to handle the exception is more difficult. The exception handling mechanism is defined in the JSP specification.
    1) compose a JSP "error page"         Exception handling file China needs to use the Iserrorpage property of the page directive: <%@ page Iserrorpage= "true"%&gt, at which point the exception object exception can be accessed.     2) specify in the JSP which error page should be responded to when an exception occurs         causes the page to cause an exception to be handled by a specialized exception handling file. You need to specify a specialized exception handling page in this page using the ErrorPage of the Pages Directive <%@ page errorpage= "Exception handling file"%>        of course, You can also configure the exception-handling file through the Web. xml file to specify a uniform exception handling file for the same type of exception.         Common exception codes:        400 Error: Error request         401 Error: Access denied         403 Error: File is forbidden         404 error: File not found         500 error: An unknown error occurred during the operation of the file: Test page index.jsp, intentionally set an exception
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>// intentionally set an exception here        string[] strlen = { "Hello", "error", "Beijing"};          for (int i = 0; i < i++) {            + "--test");            I++        ;    } %></body>

Configuring the Exception handling page in the Web. xml file

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://java.sun.com/xml/ns/javaee"         xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance"         xsi:schemalocation= "http://java.sun.com/xml/ Ns/javaee          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "         version=" 3.0 ">    <!--configured according to the exception code        -<error-page> <!--to the response Status Code declaration error page-        <error-code>500</ error-code>        <location>/500.jsp</location>    </error-page>    <!-- Configure based on exception type    -<error-page>        <!--declare error pages for specific exception types--        <exception-type> java.lang.throwable</exception-type>        <location>/500.jsp</location>    </error-page ></web-app>

Writing exception Handling page 500.jsp

<%@ page contenttype= "Text/html;charset=utf-8" language= "Java"%><%@ page iserrorpage= "true"%>This is the 500 error handling page! <br> exception information is <%=exception.getclass (). GetName ()%></body>

The execution process of JSP and its exception handling mechanism

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.