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 string and find out the length of the string using length()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. Exception.jsp
2. Example.html
//Exception.jsp
<html>
<body>
<%@ page errorPage="example.jsp" %>
Example for Null Pointer exception:
<%
String s=null;
s.length();
%>
</body>
</html>

//Error handler file
//Example.jsp
<html>
<body>
<%@ page isErrorPage="true" %>
The error is:
<%= exception %>
</body>
</html>
The output of the program is as shown in Figure 10.3.

Figure 10.3: Output of Example.jsp

0 nhận xét: