This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes A Question of Style:  Validating Field Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "A Question of Style:  Validating Field Values" Watch "A Question of Style:  Validating Field Values" New topic
Author

A Question of Style: Validating Field Values

Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
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
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Question of Style: Validating Field Values
 
Similar Threads
Encrypt form POST data before calling servlet through JSP
validation of more than one field (e.g. password verification)
jQuery-How to highlight invalid input field value after getting validation error
Retrieving an element when we only know what the id starts with
Variable not changing before form submit?