Verify the user's input using the JSP tag library (2)
Verify JSP view
Usually to be added, the programmer requires a separate JSP page that looks like the original form page, but there is an error message, and can be displayed by the server (redirection) when there is a problem with the form field. I simply adds error logic in the original JSP page. When the form is first displayed, the error check will not be executed. On the submission behavior, the form is submitted to yourself, the domain is verified by the JSP tag (on the server side, JSP is compiled into servlets). If all are correct, the data is submitted to the main controller servlet. If it is not correct, the user will see an error message in the same JSP page. In the JSP page, I created a Boolean flag variable "Validate" with Java scripts. If there is a "Validate" parameter is submitted to the JSP, set it to true. <% boolean validate = ("true" .Equals ((String) Request.GetParameter ("Validate"))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))));%> Based on this Boolean variable, JSP will use my tag to verify. When the page is first loaded, this variable is false, and the verification will not be executed. To submit a page to itself and redirect to the main controller servlet, I changed the form behavior to point to <% = request.getRequesturi ()%> and add the default forwarding tag
// Retrun Code of 1 Will Cause Tag Body To Executeif (Value == NULL)
Return this.eval_body_include; // check if we have zip code
Value.equals (null))
// check if value is not null
Return this.eval_body_includ;
IF (Value.Length () == 5) {
// Has to be an inTeger! short case of zip code xxxxx
Try {
Integer.Parseint (Value);
Return this.skip_body;
} catch (NUMBERFORMATEXCEPTION E) {
Return this.eval_body_includ;
}
Else if (Value.Length () == 10) {
// long case of zip code xxxxx-xxxx
String part1 = value.substring (0, 5);
String dash = value.substring (5, 6);
String part2 = value.substring (6);
IF (! Dash.equals ("-")))
Return this.eval_body_includ;
Try {
Integer.Parseint (Part1);
Integer.Parseint (part2);
Return this.skip_body;
} catch (NUMBERFORMATEXCEPTION E) {
Return this.eval_body_includ;
}
}
Return this.eval_body_include; // all Other Cases
Listing 2