Friday, April 17, 2009

Write a program to display an error message to the user if an exception occurs in the JSP page

Write a program to display an error message to the user if an exception occurs in the JSP page. In the JSP page, consider a null vector and find out the length of the string using size()method of Java. Create an error handler to handle the exception thrown by this JSP page.

Solution:
The files used to run the application are:
1. Nullexception.jsp
2. errorpage.jsp
//Nullexception.jsp
<%@ page errorPage="errorpage.jsp" import="java.util.Vector"%>
<html>
<head>
</head>
<%! Vector vec = null; %>
<body bgcolor=#ffffff>
Vector has <%= vec.size() %> elements.
</body>
</html>
//errorpage.jsp
<html>
<%@ page isErrorPage="true" %>
The exception <font color="red"> <%= exception.getMessage() %> </font> has occurred.
</body>
</html>

The output of the program is as shown in Figure 10.6.

Figure 10.6: Output of Nullexception.jsp

0 nhận xét: