JSP training (13) -- Exception Handling

Source: Internet
Author: User

This article describes JSP code exceptions, Java code exceptions, you can refer to another article: http://blog.csdn.net/JavaEETeacher/archive/2007/03/19/1533754.aspx

To handle possible exceptions of JSP programs, you can do so from multiple perspectives: specific code, specific files, or error types. For specific code, you can use the <C: Catch> tag in the core tag library of the standard tag library. For specific files, you can use the iserrorpage attribute and errorpage attribute in the JSP page command. For specific types of errors, you can use the web. xml configuration file. 1. Use <C: Catch> to handle exceptions<C: Catch> A tag can be specific to one or more lines of code. <C: Catch> the basic format of a tag is as follows: <C: catch [Var = "variable storing exception information"]> Other JSP code, including various tags </C: catch> its usage is very similar to the try statement in Java code. Place the code that may generate exceptions between the <C: Catch> Start mark and end mark. In this way, when the Code generates an exception, the JSP Container will handle the exception. The following is an example of how to handle interface exceptions: <C: Catch> <C: Out value = "$ {user. address} "/> </C: Catch> if you want to save the exception information for other codes after an exception is generated, you can use the VaR attribute to specify a variable. The following code stores the exception information in the exceptioninfo variable when an exception occurs: <C: Catch Var = "exceptioninfo"> <C: Out value = "$ {user. address} "/> </C: Catch> if necessary, output the following exception information: <C: If test =" $ {exceptioninfo! = NULL} "> An exception occurred during running. The exception information is: $ {exceptioninfo. Message} </C: If> 2. Specify an exception handling file for the pageIf you do not want to handle exceptions for each piece of code, you can handle exceptions on the page as a unit. You can specify when an error occurs during the running of the current page, the exception is handled by a specific page. To use this exception handling method, you need to write a special exception handling file and set it in each file that requires exception handling. Compile an exception handling fileThe iserrorpage attribute of the page command must be used in the exception handling file. The format is as follows: <% @ page iserrorpage = "true" %> if this setting is performed on the page, the page has a special function to access the exception object exception. Exception is an internal JSP object. When an exception occurs during the running of the page, an exception object exception is thrown. This object contains the exception information. The following is an exception handling file: File Name: errorpage. JSP <% @ page contenttype = "text/html; charset = gb2312" %> <% @ page iserrorpage = "true" %> page exception. The exception information is as follows: $ {exception. message} Specify an exception handling file for the pageTo handle exceptions in a page with a special exception handling file, you must use the errorpage of the page command to specify a special exception handling interface. The format is as follows: <% @ page errorpage = "exception handling file" %> assume that you want to set the previously written errorpage. the JSP page is the exception handling file of the current page. You can use the following code: <% @ page errorpage = "errorpage. JSP "%> 3. Configure through web. xmlIf you do not want to set an exception handling file for each page, you can specify a unified exception handling file for the same type of exceptions. There are still some exceptions that cannot be completed through page settings. For example, if a user inputs a non-existent file on the website, the user should be notified that the file does not exist, however, this exception cannot be solved by setting on the page. To specify an exception handling file for each type of exception, you can configure it through web. xml. You can configure in Web. XML according to the error type, such as nullpointexception and ioexception, or according to the error code. The common Exception Code is as follows: 403 error: the file is not accessible; 404 error: the file is not found; 500 error: an unknown error occurs during the file running. You can use the following code to configure the exception type: <error-page> <exception-type> JAVA. lang. nullpointerexception </exception-type> <location>/nullpointer. JSP </location> </error-page> is configured based on the Exception Code. You can use the following code: <error-page> <error-code> 401 </error-code> <location>/401.jsp</location> </error-page>

Reference material: Basic tutorial on Java Web Programming

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.