Verifying User Input Using JSP Tag Libraries
JSP Views with Validation
Usually to add validation, programmers need a separate JSP page that looks like original form page, but has error messages and is displayed (redirected to) by the server in case the form fields have problems. I simply added error logic in the original JSP page. When the form is displayed for the first time, error checking is not done. On the submit action, the form is submitted onto itself, fields are validated using JSP tags (on the server side, JSPs are compiled into Servlets), and if everything is correct, data is posted to the main Controller Servlet. If not, users will see an error message in the same JSP.
In the JSP page, I have a Java scriplet that creates a boolean flag variable "validate", and sets it to true if there is a "validate" parameter submitted to this JSP.
Based on the value of this Boolean variable, JSP will do validation with my tag. When the page is loaded for the first time, this variable is false and validation is not done.
To submit the page into itself and then redirect it to the main Controller Servlet, I changed the form action to point to <%=request.getRequestURI()%> and added the default JSP forward tag <jsp:forward page="../MainServlet" />.
When the user submits the form, it posts all values onto the same JSP, sets the validate variable to true, does checking using tags, and if data is approved, JSP forwards all values to the Controller Servlet.
If there is a problem, the tag body will execute and tell JSP to resubmit values onto itself again, showing new (or different) error messages depending on the errors. Please note that another Boolean variable "success" is set to false inside of the tag body. This variable is initially set to true and is there only to check if any tag body has been executed. This makes sure the form is forwarded only if both the "validate" and "success" variables are true. See Listing 2.
In a situation where you have multiple fields being validated, error massagers from the tag bodies will show for incorrect fields, decreasing every time user makes corrections and resubmits.
Wrong ZIP code result page:
Correct ZIP code result page:
The extra logic that I put in JSP to perform validation in the same page and combined it with use of tags allowed me to create a simple and highly reusable solution for server-side data verification, without multiple JPS or Servlets doing it. One tag is all that is necessary to validate any kind of field, and you can have specific tags for different field types; for instance, e-mail, phone, or integer-only fields. This design extends the JSP view layer of the Model-View-Controller project and by using it I also increased the separation of the presentation from the logic layers. Web designers and developers using JSP tags for validation don't have to change any code in JSP if the tag code is modified; furthermore, they don't even have to know how it validates or any Java syntax it uses. All they have to do is simply include an HTML-like tag in their JSP page.
// retrun code of 1 will cause tagbody to execute
//check ifvalue is not null
Please enter valid Zip Code
About the Author
Vlad Kofman is a System Architect working on projects under government defense contracts. He has also been involved with enterprise-level projects for major Wall Street firms and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.
"Java Servlet Technology" by Stephanie Bodoff
"Servlets and JavaServer Pages (JSP) 1.0: A Tutorial"
JSP Tag Libraries
Gal Shachor, Adam Chace, Magnus Rydin
May 2001, Manning Publications Co.
"Chapter 3: Developing your first tags"
Page 2 of 2