|Example: mustbeerror. jsp
<% @ Page contenttype = "text/html; charset = gb2312"
In this example, errorpage = "error. jsp" is used to specify the error handling page when an error occurs.
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java"Iserrorpage = "true"Import = "Java. Io. *" %>
In error. jsp, specify iserrorpage = "true" in the page command"
<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee"
Note: If the error code is 404, call/pagenotfound.html.
Because the JSP file is compiled into a servlet for execution, the default exception action is to display the exception stack. The JSP page provides the ability to override this default behavior and transfer exception handling to another file:
In this way, if any uncaptured throwable object is encountered on this JSP page, the specified error page is displayed.
Here is a specific example. First, we develop a page with errors, as shown in routine 10-10.
Page (mustbeerror. jsp)
As you can see from the code, this page will surely produce errors. In the page command on this page, use errorpage = "error. jsp" to specify the error handling page.
Let's look at the specific code of the error handling page, as shown in the 10-11 routine.
Routine 10-11 error handling page (error. jsp)
The following error occurs:
In error. jsp, specify the following in the page command:
This is critical. You can only specify this page to handle errors and use the exception object.
The exception object has several important methods used to obtain error information. These methods are as follows:
The last method is used to print the error stack. If you need to use this exception object outside the error page, you can save the exception object as follows:
On another page:
Introduce this exception object.
Enter http: // 127.0.0.1: 8080/ch10/mustbeerror. jsp in the browser. This page will automatically call the http: // 127.0.0.1: 8080/ch10/error. jsp page. The running effect is 10-3.
The preceding figure shows how to handle internal errors on the JSP page. What if exceptions are generated in JavaBean? The specific method is to throw the exception to the JSP page in the JavaBean. If the exception is not caught on the JSP page, it is also handled on the error page. Let's take another example. Suppose There Is A JavaBean used to calculate the sum of two integers, as shown in the Code 10-12 in the routine.
Routines 10-12 calculate the two integers and the JavaBean (addjavabean. Java)
In the 10-12 routine, exceptions may occur in the integer. parseint method. When this exception occurs, it is thrown to the JSP page. We can see how to use this class on the JSP page and handle exceptions, as shown in routine 10-13.
Example 10-13 JSP processing of exceptions thrown in JavaBean (Add. jsp)
From the code, we can see that the exception generated in the JavaBean is not captured on this page, so the error handling page is automatically called.
Enter http: // 127.0.0.1: 8080/ch10/Add. jsp in the browser? X = 12 & Y = 112 will run normally. Enter the following values in the form:
X = 12, y = sdkjf
A running exception is generated, which will be processed in error. jsp, as shown in Figure 10-4.
After the preceding values are input and submitted, numberformatexception is captured in error. jsp, as shown in 10-5.
Let's further refine the above practices and conclude that JSP error handling essentially transfers exceptions on different pages or classes and handles exceptions on the target page.