August 31, 2015
Hot Topics:

Verifying User Input Using JSP Tag Libraries

  • December 11, 2003
  • By Vlad Kofman
  • Send Email »
  • More Articles »

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.

<% boolean validate = ("true".equals((String)request.getParameter("validate"))); %>

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.

Download source code here.

Code Listings

Listing 1

public int doStartTag() throwsJspException {

// retrun code of 1 will cause tagbody to execute

if (value == null)

returnthis.EVAL_BODY_INCLUDE;//check if we have zip code


//check ifvalue is not null


if(value.length() ==5) {

//hasto be an integer! short case of zip code xxxxx



return this.SKIP_BODY;

} catch (NumberFormatException e){

return this.EVAL_BODY_INCLUDE;


} else if (value.length() == 10) {

// long case of Zip code xxxxx-xxxx

Stringpart1 = value.substring(0, 5);

Stringdash = value.substring(5, 6);

Stringpart2 = value.substring(6);


return this.EVAL_BODY_INCLUDE;

try {



return this.SKIP_BODY;

} catch (NumberFormatException e){

return this.EVAL_BODY_INCLUDE;



returnthis.EVAL_BODY_INCLUDE;// all other cases

Listing 2

<FORM action="<%=request.getRequestURI()%>" method="post">

<TABLE border="1">

<input type="hidden" name="ACTIONKEY" value="WeatherAction.viewByZip">

<input type="hidden" name="REDIRECTKEY" value="weather_data">

<input type="hidden" name="validate" value="true">



<TD><font face=Arial size=2>Zip:</font></TD>


<% if (validate) { %>


<!-- do something if field is empty -->

<font color=red face=arial size=2>

Please enter valid Zip Code


<% success=false; %>


<% } %>

<INPUT type="text" name="ZIP" value="<%=((validate)?request.getParameter("ZIP"):"")%>">

<% if (success && validate){ %>

<jsp:forward page="../MainServlet" />

<% } %>





<TD><INPUT type="submit" name="view" value="View"></TD>





Listing 3

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.
ISBN 193011009X

"Chapter 3: Developing your first tags"

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel