This article illustrates the method of JSP exception handling. Share to everyone for your reference. Specifically as follows:
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 property and the ErrorPage property in the JSP's page directive.
If you are targeting a specific type of error, you can use the Web.xml configuration file.
1 using <c:catch> for exception handling
<c:catch> tags can be specific to a single line or multiple lines of code. The basic format of the <c:catch> label is as follows:
Copy Code code 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.
Here is an example of how an interface exception is handled:
<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 the 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 information that you can output exceptions at a later:
<c:if test= "${exceptioninfo!= null}" >
runtime generates an exception with the exception message: ${exceptioninfo.message}
</c:if>
2 Specify an 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 when the error occurred, by a specific page to handle the exception.
With this exception handling, a special exception handling file needs to be written, and then set in each file that requires exception handling.
Writing Exception handling files
The Iserrorpage property of the page directive needs to be used in the exception handling file in the following format:
Copy Code code as follows:
<%@ page iserrorpage= "true"%>
If you make such a setting 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.
Here is an exception handling file:
FileName: errorpage.jsp
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page iserrorpage= "true"%>
The page produces an exception, and the exception information is as follows:
Copy Code code as follows:
To specify an exception handling file for a page
In order for the page to produce an exception, the exception is handled by a specialized exception-handling file, which requires the errorpage of the page directive to be specified in the pages to specify a specialized exception handling interface, in the following format:
Copy Code code as follows:
<%@ 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:
Copy Code code as follows:
<%@ page errorpage= "errorpage.jsp"%>
3 Configuration via Web.xml
If you do not want to set exception handling files for each page, you can specify a uniform exception handling file for the same type of exception. There are also some exception handling is no way through the page setup to complete, such as the user entered a Web site does not exist, this time should tell the user file does not exist, but this exception is no way through the page settings to solve.
To specify an exception handling file for each type of exception, you can configure it through Web.xml. Configuration in Web.xml can be configured according to the error type, such as Nullpointexception, IOException, or by error encoding.
The common exception encodings are as follows:
403 Error: File is forbidden to access;
404 error: File not found;
500 error: An unknown error occurred during the run of the file.
Depending on the type of exception, you can use the following code:
<error-page>
< Exception-type >java.lang.NullPointerException</exception-type>
< Location>/nullpointer.jsp</location>
</error-page>
Configured according to the exception encoding, you can use the following code:
<error-page>
<error-code>401</error-code>
<location>/401.jsp</location>
</error-page>
I hope this article will help you with the JSP program design.