HtmlToXml Tutorial 1: > Input validation from CGI FORM by XML
Previous Page Contents Next Page
OOP-Reserch home page

Contact Us

Input validation from CGI FORM by XML

Input validation by XML

Assuming that you'd like to ask the users to type their age into the text field in the CGI FORM, as shown below:

CGI FORM
[CGI FORM]

But, the user may type the alphabetical characters (a-z or A-Z). To be ready for the wrong input from the users, the destination JSP/Servlet should check the validity of the request parameters from the CGI FORM.

In case of the wrong input, the JSP/Servlet should show the appropriate message to the users. Typically, the JSP/Servlet should show the same CGI FORM again, but with the previous inputs as the default values. In addition, the JSP/Servlet should indicate which field includes the wrong input. So, the CGI FORM will look like this:
CGI FORM with invalid input
[CGI FORM with invalid input]

With this CGI FORM, the users can easily fix some of their previous inputs and send them again.
The input validation from the CGI FORM like above is required in most JSP/Servlet programming. But, as you know, it is somewhat tedious task.
Apache Struts, the very sophisticated MVC (Model-View-Controller) framework, includes the solution by: method. You will need to implement the sub-class of ActionForm and define this method for your requirement. This method is called by the Controller Servlet in Struts framework, and returns the ActionError object in case of the invalid input. The ActionError object includes the keys of MessageResource bundle. Controller Servlet retrieves the corresponding error messages from the MessageResource bundle, and inserts them into the JSP, which has: In case of JavaServer Faces, you can use the pre-defined Validator implementations, or you may need to implement your own one. And, your JSP will include the tags like: within faces:textentry_input context.

As you know, both of Apache Struts and JavaServer Faces fully depend on the custom tag libraries. This means that the web designers (or page authors) must be familiar with custom tag libraries to some extent. But, in the real world, this is not always true.

In addition, html_basic tag library in JavaServer Faces can be used only for generating the HTML response. To render the response for XHTML, WML or HDML, you will need to implement other tag libraries by yourself.

To make the things easier, OOP-Reserch tried to find another way. And we noticed the simple fact:
Who knows the acceptable data type for each input field in the CGI FORM? The answer is the CGI FORM itself.
In other words, the HTML tag for the CGI FORM is the best place to write the acceptable data type for each field, if we could.
HtmlToXml makes it possible. HtmlToXml is the HTML-to-XML conversion tool with Java Swing GUI. Please write a pair of HTML, one for the valid case and another for the invalid case. Both of them should include the HTML tag for the CGI FORM, but the latter should also include the error messages (and the additional HTML tags) for the invalid inputs. Then, HtmlToXml converter will read a pair of these HTML and generates the single XML. While in the conversion, this tool will ask you to specify the acceptable data type for each input, and merge them into the resulting XML. Please copy this XML into WEB-INF/classes directory for your web application. That's all!

In addition, HtmlToXml makes Localization (L10n, Japanese message for Japanese users and Chinese message for Chinese users) much easier than the JSP/JSTL based-solution, such as Apache Struts and JavaServer Faces. Localization (L10n) by the JSP/JSTL fully depends on the resource bundle, i.e. the localized messages should be written in the property resource files in case of Apache Struts or JavaServer Faces. As for HtmlToXml, all the localized messages can be included in the normal HTML. Localization (L10n) is now the responsibility of the web designers (or page authors), and you (Java programmer) need not prepare the additional resources for Localization (L10n).

Moreover, HtmlToXml can read any kind of mark-up language, such as such as XHTML, WML or HDML. You (Java programmer) need not concern with the View part of your web application.

Java and all Java-based trademarks and logos are trademarks or registered of Sun Microsystems, Inc. in the United States and other countries.


Previous Page Contents Next Page
OOP-Reserch home page

Contact Us


ALL CONTENTS COPYRIGHT 2002, Jun Inamori. All rights reserved.
Any questions and comments are welcome to Jun Inamori .