• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Question of Style: Validating Field Values

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, friends.
I'm working on a simple servlet that accepts values from HTML text inputs and updates a database. Examples of these fields include user ID, password and ZIP code. I would like to validate these fields, at least rudimentarily. For example, I would like the user ID and password to have minimum lengths, I'd like to make sure that these two fields do not equal each other, I'd like to limit the ZIP code to five digits, etc. If the user incorrectly enters any field and then clicks "Submit," my servlet returns the user to the same page, displaying an appropriate error message written by yours truly. What is the best O.O. way to validate (edit-check) these fields?
Right now, in my doPost() method, I call a validate() method. Within validate(), I call many sub-methods like isZipValid() and isUserIdAvailable(). In case of error, these sub-methods throw a custom-made exception containing my error-specific text back to validate(). But all these try/catch blocks in validate() make me think that there has to be a better way. I'd like to suit my error messages to the infraction of the user, but also keep my code object-oriented.
Can anyone point me to the "normal" way of doing this?
Thanks a lot,
Art
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if it's "normal", but...
I would prefer not to persist data into a database from a JSP or (controller) servlet, but use an intermediate object which models the real world entity. For instance, call it "user".
Now this object can be anything - if you're using a two tier model, probably a data aware bean to make it easy to access from a JSP. If you're using n-tier then it might be an EJB.
Knowledge of the precise requirements for the attributes of that object should be isolated in the object class. In other words, the "setZip" method of the "User" class would indicate an error - throw a suitable exception, say - when you would try to set an illegal zip code. (Of course, "User" may delegate this internally to a "ZipCode" instance, and "ZipCode" may internally use a "validate" method).
The controller would catch these exceptions and turn them into comprehensible error messages. If you don't mind reporting on a single error at a time, one try block with a couple of catches can do the job.
- Peter
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic