Java Reference
In-Depth Information
with the current JSP, we make use of an attribute of the page directive that we have
not yet encountered: errorPage . For example:
<%@ page errorPage="MyErrorPage.jsp" %>
To illustrate the use of such a page, the AdderServlet from Chap. 8 will now be
converted into a JSP. As in previous examples, all JSP-specifi c code will be
emboldened.
Example
Note the specifi cation of the associated error page in the second line of code
below.
<!-- Adder.jsp -->
<%@ page errorPage="NumError.jsp" %>
<%
String value1 = request.getParameter("Num1");
String value2 = request.getParameter("Num2");
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int sum = num1 + num2;
%>
<HTML>
<HEAD>
<TITLE>Result</TITLE>
</HEAD>
<BODY>
<BR><BR><BR>
<CENTER><H1><FONT COLOR='blue'>
<%= "Result = " + sum %>
</FONT></H1></CENTER>
</BODY>
</HTML>
The initial Web page (originally called SimpleAdder.html ) will need to have the
URL of its form's ACTION attribute modifi ed so that it refers to our JSP, of course:
<FORM METHOD=POST ACTION=" Adder.jsp ">
This opening fi le will itself be renamed SimpleAdder X .html .
All that remains now is to specify the code for the error page itself. This fi le must
use attribute isErrorPage of the page directive to specify its error page status.
This attribute is a Boolean value and should be set to the value true , specifi ed as a
string (i.e., enclosed by speech marks):
<%@ page isErrorPage="true" %>
In this simple application, it is highly likely that the error that has been generated
has been caused by the user entering invalid (i.e., non-numeric) data. This being the
Search WWH ::




Custom Search