JBuilder2005 Practical JSP Error Handling (4)
Author:Eve Cole
Update Time:2009-07-02 17:09:47
Error handling page error.jsp
Web applications generally have one or more unified error handling JSP pages so that when an error occurs in a functional JSP page, feedback can be provided to the user in a friendly form. Friendly and unified error pages are an aspect that cannot be ignored in the Web presentation layer.
Next, we create the error handling error.jsp file, whose code is as follows:
Code Listing 11 error.jsp error handling JSP page
1. <%@page contentType="text/html; charset=GBK" isErrorPage="true" %> 2. <html> 3. <head> 4. <title>error</title> 5. </head> 6. <body bgcolor="#ffffff"> 7. Sorry, a system exception occurred. Click <a href="login.jsp">here</a> to return to the home page. 8. </body> 9. </html> |
The isErrorPage attribute in the page directive tag of the error handling JSP page should be set to true, as shown in line 1, so that the exception implicit object can be accessed in the JSP page. On line 7, we report the program error to the user in an "elegant" way and provide a link back to the login page.
Now, let's go back and specify error.jsp as the error handling page of switch.jsp and login.jsp through <%@ page errorPage="Error Handling JSP"%>.
After adding an error handling page to the switch.jsp page, its code is as follows:
Code Listing 12 switch.jsp adds error handling Jsp page
1. <%@page contentType="text/html; charset=GBK" errorPage="error.jsp"%> 2. <%@page import="bookstore.*"%> 3. <%@page import="java.sql.*"%> 4.… |
After adding the error handling JSP page to the login.jsp page, its code is as follows:
Code Listing 13 login.jsp adds error handling page
1. <%@page contentType="text/html; charset=GBK" errorPage="error.jsp"%> 2. <%@page errorPage="error_error.jsp"%> 3.… |
In this way, when the SQL query statement of switch.jsp is incorrect and triggers a SQLException, the obscure exception trace information error page disappears and is replaced by the following friendly error page:
Figure 15 Friendly error handling page |
Login failed fail.jsp
When the user provides an incorrect password, switch.jsp will redirect to the fail.jsp page. Similarly, you can create the fail.jsp page through the JSP wizard. The code for fail.jsp is as follows:
Code Listing 14 fail.jsp login failure page
1. <%@ page contentType="text/html; charset=GBK" errorPage="error.jsp" %> 2. <html> 3. <head> 4. <title> 5.fail 6. </title> 7. </head> 8. <body bgcolor="#ffffff"> 9. The password you entered is incorrect, click <a href="login.jsp">here</a> to return to the login page. 10. </body> 11. </html> |
When the user enters an incorrect password, the effect of the fail.jsp page seen is as shown below:
Figure 16 fail.jsp page effect |