aspose file tools*
The moose likes Servlets and the fly likes OO Form Validation and Error Checking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "OO Form Validation and Error Checking" Watch "OO Form Validation and Error Checking" New topic
Author

OO Form Validation and Error Checking

Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
I was looking at a flyer for the JavaTech Summit at the WebBuilder Conference in New Orleans next month, and noticed one particular seminar that caught my eye because I'll need to be writing code to do this soon: OO Form Validation and Error Checking with JSPs. Does anyone know of any existing JSP or generic libraries that do this? Or does anyone have a snippet of example code they'd be willing to share? Or a book with examples to recommend?
The main things that I see the need for in validating forms are checking for required (not null) fields, numeric-only or text-only, min/max length, and illegal or program-busting characters. Oh, and some class or something that recognizes encoded characters and converts them back to ASCII (such as %20 (percent twenty),   (non-breaking space), etc).
Quick tech question: when doing validation, do most people post back to the same JSP when the form is submitted, and then when it finally checks out OK just forward request to a different JSP or servlet?
Thanks!


CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Check out Apache Struts at http://jakarta.apache.org/struts. One of the features to come in version 1.1 is Dave Winterfeld's Validation library that adds the kind of features you're looking for. A link to that can be found here http://home.earthlink.net/~dwinterfeldt/index.html
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Toyin Sogeke
Greenhorn

Joined: Oct 08, 2001
Posts: 14
Hi,
On the 'Tech Question' bit
Why not just use javascript to validate your form(JSP and/or
HTML page) before submitting it.
Cheers
Originally posted by Gerry Giese:
I was looking at a flyer for the JavaTech Summit at the WebBuilder Conference in New Orleans next month, and noticed one particular seminar that caught my eye because I'll need to be writing code to do this soon: OO Form Validation and Error Checking with JSPs. Does anyone know of any existing JSP or generic libraries that do this? Or does anyone have a snippet of example code they'd be willing to share? Or a book with examples to recommend?
The main things that I see the need for in validating forms are checking for required (not null) fields, numeric-only or text-only, min/max length, and illegal or program-busting characters. Oh, and some class or something that recognizes encoded characters and converts them back to ASCII (such as %20 (percent twenty), � (non-breaking space), etc).
Quick tech question: when doing validation, do most people post back to the same JSP when the form is submitted, and then when it finally checks out OK just forward request to a different JSP or servlet?
Thanks!


homeboy
Joe Pardi
Ranch Hand

Joined: Oct 03, 2001
Posts: 47
Check out a tag library called dotJ. It has exactly what you're looking for (http://www.dotjonline.com/taglib/form.jsp). It works exactly like the new ASP.Net form controls.
I always post a form back to itself since it makes it easy to redraw the form with error indicators when any validation fails.
If the validation suceeds, forward or redirect to the next page. If the validation fails, let the page redisplay with the appropriate error messages.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Regarding the tech question, like Toyin said, Most people use JavaScript to do the front end validation. We also put the same validation on the back end here where I work.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Toyin & Bosun:
Can't do Javascript: the government organization I'm working for handed down an edict that all logic processing is server-side. This is partially for security reasons, but also for browser support - our official common desktop just moved from NS to IE but we have to support versions back to 3.x of both on PC/Mac/Unix as well as some misc. other. On the security side, the 'experts' want to block all Javascript on the network at some point. They already block all in/out Javascript and Applets at the internet firewall, as well as ActiveX, VBScript, etc, which means our remote extranet users wouldn't be able to use our apps if we used javascript.
From a practical point of view on security, even if you validate client-side with Javascript, it's still important to validate server-side. Anyone with a little time on their hands or a purpose can save and edit a page and change the script and post bad or malicious data to your site if you don't re-check server-side. I've seen it happen numerous times at various places - mostly with people trying to 'cheat'.
Kyle & Joe:
I'll go check those out - thanks for the links! I hope that Dave's Validation library is available seperately. Struts is good but we've already gone another direction on a templating engine. Haven't done JSP tags yet, so I'll have to try that out. Should work with our setup since we're still JSP-based.
Thanks all! Anyone else have 2 cents to add?
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Another reason not to use javascript: especially on large forms (we have lots) it can double or more the downloaded page size. Slow page loads are always bad and can amplify the reduced response time of a slow or loaded server.
Andre Moo
Greenhorn

Joined: Sep 29, 2001
Posts: 22
One more quick thing...
In my oppinion, for robust code its best to implement both client-side and server-side form validation for redundancy. Bugs appear quicker and you can't always guarantee that the user will have client-side scripting available/enabled. In your case this decision has been made for you.
As the guys said above, Struts is a good bet although, it is about a lot more that just form validation.
If you prefer to read things on paper than on the web then you might like to buy "Advanced JavaServer Pages" by David M. Geary (ISBN 0-13-030704-1) http://www.phptr.com/advjsp
This book covers OO form validation and a full blown form framework. It also covers a whole host of other indispensible topics on web application frameworks etc. The author David M. Geary is one of the Committers on the Jakarta Struts project and unsurprisingly the techniques described in the book are strikingly similar to the implementation of Struts!!!
However, it has the advantage of letting you read about and understand just the bits you want - form validation - and ignore the rest of the framework.
On a side note. The CURRENT version of Struts is far from perfect, especially when it comes to client-side validation - but you don't need to worry about that. There are also other viable application frameworks out there.
Andre.
PS.
On your tech question, Struts might lead you to take a slightly different approach.
 
wood burning stoves
 
subject: OO Form Validation and Error Checking