Exception handling of JSP training

Source: Internet
Author: User
Tags exception handling

to handle the exceptions that a JSP program might produce, you can do so from multiple angles: You can target specific code, you can target specific files, or you can target the wrong type.


If you are targeting specific code, you can use the <c:catch> label in the core tag library in the standard tag library.


If you are targeting a specific file, you can do so by using the Iserrorpage and ErrorPage properties in the JSP's page directive.


you can use the Web.xml configuration file for a specific type of error.


1 uses <c:catch> for exception handling


<c:catch> tags can be specific to one line or multiple lines of code. The basic format of the <c:catch> label is as follows:


<c:catch [var= "variables that store exception information"]>


other JSP code, including a variety of tags


</c:catch>

The
usage is very similar to a try statement in Java code, where the code that might produce an exception is placed between the <c:catch> start and end flags. Thus, when the code produces an exception, the JSP container handles the exception.


Below is an example of handling an interface exception:


<c:catch>


<c:out value= "${user.address}"/>


</c:catch>


If you want to create an exception, save the exception information for use by other code, you can use the Var property to specify a variable.


the following code stores exception information in the Exceptioninfo variable when the exception is generated:


<c:catch var= "Exceptioninfo" >


<c:out value= "${user.address}"/>


</c:catch>


If you need to be able to output exception information at a later:


<c:if test= "${exceptioninfo!= null}" >


Runtime generates an exception, the exception message is: ${exceptioninfo.message}


</c:if>


2 Specifies the exception handling file for the page


If you do not want to deal with each piece of code exception handling, you can use the page as a unit for exception handling, you can specify the current page to run the process of error, by a specific page to handle the exception.


This exception handling, you need to write a special exception handling file and then set it in each file that requires exception handling.


Write exception handling files

The iserrorpage attribute of the page directive needs to be used in the
exception handling file in the following format:


<%@ page iserrorpage= "true"%>


If this setting is done on the page, the page has a special function to access the exception object exception. Exception is the internal object of the JSP, when the page is in the process of producing an exception, throws an exception object exception, the object contains exception information.


Below is an exception handling file:


filename: errorpage.jsp


<%@ page contenttype= "text/html;charset=gb2312"%>


<%@ page iserrorpage= "true"%>


Page produces an exception, the exception information is as follows: ${exception.message}


Specifies the exception handling file for the page


If you want the page to produce an exception, the exception is handled by a special exception-handling file, where you need to specify a specialized exception handling interface using the page directive's ErrorPage, in the following format:


<%@ page errorpage= "Exception handling file"%>


Suppose you want to set the errorpage.jsp page that you wrote earlier as the exception handling file for the current page, you can use the following code:


<%@ page errorpage= "errorpage.jsp"%>


3 is configured via Web.xml

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.