JSP learning Exception Handling instance analysis and Exception Handling instance analysis
This document describes how to handle JSP exceptions. Share it with you for your reference. The details are as follows:
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:
Copy codeThe Code is as follows: <c: catch [var = "variable storing exception information"]>
Other JSP code, including various tags
</C: catch>
The usage is similar to the try statement in Java code. Place the code that may generate exceptions between the <c: catch> Start flag and end flag. 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 code after an exception is generated, you can use the var attribute to specify a variable.
The following code stores the exception information in the predictioninfo variable when an exception occurs:
<c:catch var="exceptionInfo"> <c:out value="${user.address}"/></c:catch>
If necessary, you can output the exception information below:
<C: if test = "$ {predictioninfo! = Null} "> An exception occurred during running. The exception information is: $ {exceptionInfo. message} </c: if>
2. Specify an exception handling file for the page
If 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 file
The isErrorPage attribute of the page command must be used in the exception handling file. The format is as follows:
Copy codeThe Code 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:Copy codeThe Code is as follows: $ {exception. message}
Specify an exception handling file for the page
To 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:
Copy codeThe Code is as follows: <% @ page errorPage = "exception handling file" %>
If you want to set the previously written errorPage. jsp page as the exception handling file of the current page, you can use the following code:
Copy codeThe Code is as follows: <% @ page errorPage = "errorPage. jsp" %>
3. Configure through web. xml
If 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.
Common exception codes are as follows:
403 error: access to the file is forbidden;
404 error: the file is not found;
500 error: Unknown error occurs during 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>
You can use the following code to configure the code based on the Exception Code:
<error-page> <error-code>401</error-code> <location>/401.jsp</location></error-page>
I hope this article will help you with JSP program design.