Example: mustbeerror. jsp <% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *, javax. servlet. *, javax. servlet. http. *" errorpage = "error. jsp" %> <% // This page must have an error. Int I = 0; Int J = 1; Out. println (J/I ); %> In this example, errorpage = "error. jsp" is used to specify the error handling page when an error occurs. --------------------------- Error. jsp <% @ Page contenttype = "text/html; charset = gb2312" Language = "Java"Iserrorpage = "true"Import = "Java. Io. *" %> <HTML> <Head> <Title> error! </Title> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> </Head> <Body> Error! <Br> The following error occurs: <Br> <HR> <font color = Red> <HR> Getmessage (): <br> <% = Exception. getmessage () %> <br> <HR> Getlocalizedmessage (): <br> <% = Exception. getlocalizedmessage () %> <br> <HR> Printstatcktrace (): <br> <% Stringwriter Sw = new stringwriter (); Printwriter PW = new printwriter (SW ); Exception. printstacktrace (PW ); Out. println (SW ); %> <Br> </Font> </body> </Html> In error. jsp, specify iserrorpage = "true" in the page command" <----------------------> Declare the exception and error page in Web. xml For example: <? XML version = "1.0" encoding = "ISO-8859-1"?> <Web-app xmlns = "http://java.sun.com/xml/ns/j2ee" Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd" Version = "2.4"> <Error-page> <Error-code> 404 </error-code> <Location>/pagenotfound.html </location> </Error-page> <Error-page> <Exception-type> JAVA. Lang. numberformatexception </exception-type> <Location>/numberformatexception.html </location> </Error-page> </Web-app> Note: If the error code is 404, call/pagenotfound.html. You can also specify the error type such as Java. Lang. numberformatexception. The error type is called/numberformatexception.html. From: http://www.zhuoda.org/air_tuyh/26516.html ====================================== Http://www.51cto.com/art/200704/46521.htm 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:
<%@ page errorPage="error.jsp"%>
|
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)
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *, javax. servlet. *, javax. servlet. http .*" Errorpage = "error. jsp" %> <% // This page will definitely fail Int I = 0; Int J = 1; // The following code must generate a runtime error Out. println (J/I ); %>
|
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)
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Iserrorpage = "true" Import = "Java. Io. *" %> <HTML> <Head> <Title> error! </Title> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> </Head> <Body> Error! <Br>
|
The following error occurs:
<br>getMessage():<br> <%=exception.getMessage()%><br>getLocalizedMessage():<br> <%=exception.getLocalizedMessage()%><br>PrintStatckTrace():<br> <% StringWriter sw=new StringWriter(); PrintWriter pw=new PrintWriter(sw); exception.printStackTrace(pw); out.println(sw); %><br> </font></body>
|
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:
<%=exception.getMessage()%> <%=exception.getLocalizedMessage()%> <% exception.printStackTrace(PrintWriter);%>
|
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:
session.setAttribute("myError",exception);
|
On another page:
Exception exception=( Exception )session.getAttribute("myError");
|
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.
|
Figure 10-3 custom error page |
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)
Package com. jspdev. ch10; // Calculate the sum of two integers. An exception may occur in the integer. parseint method. Public class addjavabean { Public int add (string X, string y) throws exception { Int ret = 0; Try { Ret = integer. parseint (x) + integer. parseint (y ); } Catch (exception E) { Throw E; } Return ret; } }
|
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)
<%@ page contentType="text/html; charset=gb2312" import="com.jspdev.ch10.*" language="java" errorPage="error.jsp" %> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <body> <center> <% String x=request.getParameter("x"); String y=request.getParameter("y"); AddJavaBean addBean=new AddJavaBean(); %> <%=x%>+<%=y%>=<%=addBean.add(x,y)%> add.test<br> <form action="add.jsp" method="get"> <table><tr><td>x: <input type="text" name="x"></td></tr> <tr><td>y:<input type="text" name="y"></td></tr> <tr><td><input type="submit" value=add></td></tr> </table> </form> </center> </body>
|
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.
|
Figure 10-4 inputting exception information in Add. jsp |
|
Figure 10-5 Handling exceptions in Add. jsp in error. jsp |
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. |