So far, we still have some questions that have not been clarified. First, you cannot clear the name area. If you type an incorrect content (not an integer value) in the "age" area, a Java Exception error occurs.
Now we will solve this problem using other flags in the Blazix flag library. Blazie provides a blx: setProperty flag to solve this problem. Blx: setProperty allows us to define an exception error handling method. If an exception occurs, we can collect an error message for the user and continue to process it.
Below is our modified SaveName. jsp, which can handle any errors. It not only displays GetName. jsp, but also can type the correct data on it, and automatically connects to NextPage. jsp:
<% @ Taglib prefix = "blx" uri = "/blx. tld" %>
<%!
Boolean haveError;
StringBuffer errors;
Public void errorHandler (String field,
String value,
Exception ex)
{
HaveError = true;
If (errors = null)
Errors = new StringBuffer ();
Else
Errors. append ("<P> ");
Errors. append ("<P> Value for field" "+
Field + "" is invalid .");
If (ex instanceof java. lang. NumberFormatException)
Errors. append ("The value must be a number .");
}
%>
<%
// Variables must be initialized outside declaration!
HaveError = false;
Errors = null;
%>
---------------
<BODY>
<Jsp: useBean id = "user" class = "UserData" scope = "session"/>
<Blx: setProperty name = "user"
Property = "*"
OnError = "errorHandler"/>
<%
If (haveError ){
Out. println (errors. toString ());
PageContext. include ("GetName. jsp ");
} Else
PageContext. forward ("NextPage. jsp ");
%>
</BODY>
</HTML>
Note that haveError and errors must be reinitialized each time because they are initialized outside the declared environment.
Now, if an error occurs in the blx: setProperty processing process, we will display this error and return to GetName. jsp again so that users can correct this error. If no error occurs, we automatically connect the user to NextPage. jsp.