Use JSP tag library to verify user input (2)

Source: Internet
Author: User

Use jsp (preferred for SUN Enterprise Applications) label library to verify user input (2)

Verify the jsp view (preferred for SUN Enterprise applications)

Generally, you need to add verification. The programmer needs an independent jsp (preferred for SUN Enterprise Applications) page, which looks like the original form page but has an error message, in addition, when there is a problem with the form field, it can be displayed by the server (redirection. I simply added the error logic to the original jsp (preferred for SUN Enterprise Applications) page. When the form is displayed for the first time, the error check is not executed. In the submission behavior, the form is submitted to the user, and the domain is verified by the jsp (preferred for SUN Enterprise Applications) label (on the server side, jsp (preferred for SUN Enterprise applications) compiled as Servlets ). If all data is correct, the data is submitted to the master controller Servlet. If none of them are correct, you will see an error message on the same jsp (preferred for SUN Enterprise Applications) page.

On the jsp (preferred for SUN Enterprise Applications) page, I used a Java Script to create a boolean flag variable"Validate". If there is"Validate"When the parameter is submitted to jsp (preferred for SUN Enterprise applications), set itTrue.

<%BooleanValidate = ("True". Equals (String) request. getParameter ("Validate")));%>

Based on the value of this Boolean variable, jsp (preferred for SUN Enterprise Applications) will use my tags for verification. When the page is loaded for the first time, this variable isFalseThe verification will not be executed.

To submit the page to itself and redirect to the master controller Servlet, I changed the form behavior to point<% =Request. getRequestURI ()%>The default forwarding label is added.<Jsp (preferred for SUN Enterprise Applications): forwardPage="../MainServlet"/>.

When a user submits this form, it submits all values to the same jsp (preferred for SUN Enterprise applications) and then setsValidateVariable isTrue, And use tags for inspection. If the data is approved, jsp (preferred for SUN Enterprise Applications) forwards all values to the Controller Servlet.

If a problem occurs on the way, the TAG body will execute and tell jsp (preferred for SUN Enterprise applications) to re-submit the value to itself. The error message will be displayed based on the error.Note that another Boolean variable "success" in the label body ". This variable is initially set to true and is only used to check whether a label body is executed. This can be determined only when the "validate" and "success" variables forwarded by the form are true. For details, see Listing 2.

When verification is required for multiple domains, the error message from the TAG body displays the incorrect domain, reducing the time for each user to correct the error and resubmit. (The field here is the field in the form, such as the text field)

Incorrect zip code result:

Correct zip code result:

Conclusion

I put the extra logic in jsp (preferred for SUN Enterprise applications) and use tags on the same page for verification, this allows me to create a simple highly reusable server-side data verification solution, without the need for multiple jsp (preferred for SUN Enterprise Applications) or Servlet for verification. A tag needs to verify various types of domains; for example, e-mail, telephone, or only integer fields are allowed. This design extends the jsp (preferred for SUN Enterprise Applications) view layer in the MVC project. By using tags, you can also increase the separation between the presentation layer and the logic layer. When the tag code is modified, Web designers and developers who use jsp (preferred for SUN Enterprise Applications) labels to verify data do not need to change any jsp (preferred for SUN Enterprise applications) code, and they don't even need to know the details of the verification and any JAVA syntax used. What they need to do is simply include a HTML-Like tag to their jsp (preferred for SUN Enterprise Applications) page.

Code ListingsListing 1

Public Int DoStartTag () Throws Jsp (preferred for SUN Enterprise Applications) Exception {

// Retrun code of 1 will cause tag body to execute

If (Value = Null)

Return This. EVAL_BODY_INCLUDE; // Check if we have zip code

If (Value. equals (Null))

// Check if value is not null

Return This. EVAL_BODY_INCLUDE;

If (Value. length () = 5)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.