Verify the user's input (2) using the JSP tag library

zhaozj2021-02-16  57

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 . When the user submits this form, it submits all values ​​to the same JSP, then set the validate variable to true, and use the label to check. If the data is reviewed, the JSP forwards all values ​​to the controller servlet. If you encounter problems during the way, the label will execute and tell the JSP to re-submit the value to itself, accompanying the error message according to the error. Please note that another Boolean variable "Success" in the label body. This variable is initially set to True, just to check if there is a label being executed. This can only be determined if the "Validate" and "Success" variables that are turned forward are True. Please see Listing 2 for details. In the case where multiple domains need to be verified, the error message from the tab will display this incorrect domain, thereby reducing the time for each user correcting errors and re-submission. (Here are the domains in the form, such as text domain, etc.) Errors Postal Code Results: Correct Postal Code Results: Conclusion I put in additional logic in JSP, use tags in the same page to perform verification, so that I have made me Create a simple highly reusable server-side data authentication solution without having multiple JSP or servlets to perform verification. A label is to verify various types of domains; for example, e-mail, phone, or domains of integer values. This design extends the JSP view layer in the MVC project. By using a label, it is also possible to increase the separation of the layer and the logic layer. When the label code is modified, use the JSP tag to verify that the Web designer and developers of the data do not have to change any JSP code; and they don't even know the details of the verification and any Java syntax used. What they have to do is simply containing a label similar to HTML to their JSP page. Code Listings Listing 1 Public Int DostartTag () THROWS JSPEXCEPTION {

// 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

zip:

<% IF (Validate) {%>

">

Please Enter Valid Zip Code

<% Success = false;%>

<%}%>

">

<% IF (Success && Validate) {%>

<%}%>

Listing 3

Translated by Willpower, 2004.1.4

转载请注明原文地址:https://www.9cbs.com/read-24920.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.032, SQL: 9