OOP-ResearchMake It Simpler by Object Oriented Programming

Source code example of OOP FormGenerator API (Part 2) / Input validation from CGI FORM / Localization (L10n) / Simpler than JSP with JSTL, Apache Struts or JavaServer Faces

Source code example for Localization (L10n) of Servlet. Simpler than JSP/JSTL, Apache Struts or JavaServer Faces. Japanese Shift_JIS, Chinese BIG5 or GB2312. Input validation from CGI FORM.
This is the second example of OOP FormGenerator API. In this example, all the web GUI (or View part in MVC (Model View Controller) are generated from just the normal HTML or any other mark-up languages (such as XHTML, WML or HDML). Unlike Apache Struts or JavaServer Faces, the web designers (or page authors) need not learn even a bit of the custom tag libraries.
In case of the JSP/JSTL based solution like Apache Struts or JavaServer Faces, the localized message should be written in the property resource file for the expected Locale. In the framework design by OOP FormGenerator API, Localization (L10n) of Servlet can be very easy. For each expected Locale (language preference), the localized messages can be written in the normal HTML. So, there is not difficulty even in the 2 bytes characters such as Japanese Shift_JIS, Chinese BIG5 or GB2312.
Once the HTML is prepared, the Java Swing GUI based conversion tool generates the XML. While in the conversion, you will be asked to specify the acceptable data type for each request parameter from the CGI FORM. By this XML, a few lines of codes will be enough for the input validation in your Servlet. Compared with JSP/JSTL based solution (such as Apache Struts or JavaServer Faces), the source code of your Servlet will be much simpler.

OOP FormGenerator is deprecated. Please use OOP ViewGenerator instead of this API.

Related Pages:


Try this example and download the required APIs...

Please download the source code of this example:

Along with this source code example, please also download OOP FormGenerator API, which is available at Java API Shop by OOP-Research. There, you can select the 30 days FREE trial version. After you download the distribution of FormGenerator API, please copy its 2 JAR files (formgen.jar and oop_util_1_x.jar) into this example. For details, please read the next section.

About this source code example...

When you download and extract the distribution of this example, you will find the directory tree like below:

formgen_ex_2/formgen_2 This is docBase directory of this example Servlet. Please install this directory into your Tomcat, Jetty or any other JSP/Servlet server.
formgen_ex_2/formgen_2
  • /WEB-INF/lib
To this directory, please copy the 2 JAR files (formgen.jar and oop_util_1_x.jar) of FormGenerator API.
formgen_ex_2/formgen_2
  • /WEB-INF/classes
In this directory, you will find:
  • Property files for FormGenerator API
  • Compiled class file for this example Servlet
  • XML which will be read by this example Servlet
Only if you use the trial version of FormGenerator API, please edit:
  • WEB-INF/classes/FormGenerator.properties
and specify your serial ID.
formgen_ex_2/src The source code of this example Servlet. You need not compile these source code, because the pre-compiled class files are already installed under:
  • formgen_ex_2/formgen_2/WEB-INF/classes
But, these source code will be the good starting point of your Servlet programming with this Java API.
formgen_ex_2/html The original HTML. All the XML in this example are generated from these HTML. (Please read the next.) These HTML are just the normal HTML and there is no tricks.

For example, if you extract the distribution of this example under:
  • /home/foo
you will find the directories below:
  • /home/foo/formgen_ex_2
  • /home/foo/formgen_ex_2/formgen_2
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/lib
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/classes
First of all, please copy formgen.jar (in case of the trial version, it is formgentry.jar) and oop_util_1_x.jar (these JAR files are included in the distribution of FormGenerator API) into:
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/lib
Please edit:
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/classes/FormGenerator.properties
and specify your serial ID (which will be sent to you by e-mail.). Then, please point:
  • /home/foo/formgen_ex_2/formgen_2
as docBase of the web application context for this example. This can be done by editing server.xml if you use Apache Tomcat. Your server.xml for your Apache Tomcat will include the elements like this:


   <Context path="/formgen_2" 
      docBase="/home/foo/formgen_ex_2/formgen_2" 
      crossContext="false"
      debug="0" 
      reloadable="false"> 
   </Context>


It's time to re-start your Apache Tomcat. You can try this example on your local machine at the URL of:
  • http://localhost:8080/formgen_2/index.html
or the similar one.

The second example: CGI FORM for telephone number...

This is the second example of FormGenerator API and it is the slight modification of the first-step example. So, please read/download/try it at the URL of:

before this example. In this example, we add the additional text input to the CGI FORM of the first example. The new text field in this example asks the user to enter the telephone number.

This source code example consists of 2 Servlet:
  • example.formgen2.Cgi
  • example.formgen2.Verify
The source code of these 2 Servlet are same as the first example, i.e. the first Servlet shows the empty CGI FORM and the second one verifies the request parameters from the CGI FORM. If some of the request parameters are invalid, the second Servlet shows the same CGI FORM again. But, the CGI FORM at this time includes all the previous request parameters in it as the default values. In addition, the wrong inputs are indicated by the red colored fonts. So, the user can easily fix the input and send the request again.

By the help of FormGenerator API, all the response can be written in the XML. You can prepare the response in as many languages as you like, including the 2 bytes characters such as Japanese and Chinese. But, the distribution of this example includes the XML only in English and French, because the characters in English language and French language can be rendered on most web browsers in the world.
By the way, the expected pattern of the telephone number differs with the countries. In U.S., the telephone number is something like 123-456-7890:
Input validation for U.S.
[Input validation for U.S.]

But it should be 12-34-56-78-90 in France:
Input validation for France
[Input validation for France]

This means the different algorithm must be applied to the input validation for each Locale. It may sound somewhat difficult. But, don't worry! Because the acceptable data type can also be written in the XML, FormGenerator API makes the things very easy.

Input validation for telephone number...

In the framework by FormGenerator API, all the input validation can be done by the implementation of:

  • com.oopreserch.web.FormValueVerifier
interface. This interface has only one method:


    public boolean isValidString(String str)  


As default, FormGenerator API includes many usefull implementations of this interface. They are:

API DocSource
com.oopreserch.web.IsAlphabet[View source code]
com.oopreserch.web.IsAlphabetLower[View source code]
com.oopreserch.web.IsAlphabetUpper[View source code]
com.oopreserch.web.IsBasicLatin[View source code]
com.oopreserch.web.IsDigit[View source code]
com.oopreserch.web.IsDigitOrAlphabet[View source code]
com.oopreserch.web.IsEmailAddress[View source code]
com.oopreserch.web.IsTelephone[View source code]
com.oopreserch.web.IsJpFullWidth[View source code]
com.oopreserch.web.IsJpFullWidthKatakana[View source code]
com.oopreserch.web.IsJpHalfWidth[View source code]
com.oopreserch.web.IsJpHalfWidthKatakana[View source code]
com.oopreserch.web.IsJpHiragana[View source code]
com.oopreserch.web.IsJpPostalAddress[View source code]
com.oopreserch.web.IsJpPostalCode[View source code]

In case that these implementations are not enough, you will need to define your own implementations for your requirement. To make you life easier, FormGenerator API includes:
  • com.oopreserch.web.AbstractVerifierByRegEx
This is the abstract class which implements:
  • com.oopreserch.web.FormValueVerifier
interface. By this abstract class, you can take the full advantage of java.util.regex package and your implementation can be very simple one. To verify the request parameter for the telephone number in U.S., this example defines:
  • example.formgen2.IsTelephoneUs
Now, let's look into its source code:


public class IsTelephoneUs
    extends AbstractVerifierByRegEx{

    public void init(){
	REGEX="[0-9]{3}(\\-)[0-9]{3}(\\-)[0-9]{4}";
    }

}


That's all! Yes, what you need is just to define init( ) method. For the telephone number in France, this example includes:
  • example.formgen2.IsTelephoneFrance
Its source code looks like this:


public class IsTelephoneUs
    extends AbstractVerifierByRegEx{

    public void init(){
	REGEX="[0-9]{2}(\\-)[0-9]{2}(\\-)[0-9]{2}(\\-)
               [0-9]{2}(\\-)[0-9]{2}";
    }

}


These 2 classes should be listed in:
  • (docBase)/WEB-INF/classes/FormValueVerifierFactory.properties
along with the default implementations. It looks like this:


ANY=com.oopreserch.web.AcceptAny
DIGIT=com.oopreserch.web.IsDigit
DIGIT_ALPHABET=com.oopreserch.web.IsDigitOrAlphabet
ALPHABET=com.oopreserch.web.IsAlphabet
ALPHABET_LOWER=com.oopreserch.web.IsAlphabetLower
ALPHABET_UPPER=com.oopreserch.web.IsAlphabetUpper
BASIC_LATIN=com.oopreserch.web.IsBasicLatin
POSITIVE_INT=com.oopreserch.web.IsInteger
POSITIVE_DOUBLE=com.oopreserch.web.IsDouble
JP_ZENKAKU=com.oopreserch.web.IsJpFullWidth
JP_ZENKAKU_KATAKANA=com.oopreserch.web.IsJpFullWidthKatakana
JP_HANKAKU=com.oopreserch.web.IsJpHalfWidth
JP_HANKAKU_KATAKANA=com.oopreserch.web.IsJpHalfWidthKatakana
JP_HIRAGANA=com.oopreserch.web.IsJpHiragana
JP_POSTAL_ADDRESS=com.oopreserch.web.IsJpPostalAddress
EMAIL=com.oopreserch.web.IsEmailAddress
JP_POSTAL_CODE=com.oopreserch.web.IsJpPostalCode
TEL=com.oopreserch.web.IsTelephone

TEL_US=example.formgen2.IsTelephoneUs
TEL_FRANCE=example.formgen2.IsTelephoneFrance


Thus, whenever you define your own implementation of:
  • com.oopreserch.web.FormValueVerifier
interface, please add its fully qualified class name to this property file. The keys in this property file can be anything as long as they are unique. In this example, they are TEL_US and TEL_FRANCE.
All the keys in this property file will appear in the combo box (pull down list) as a series of acceptable data types, while you converting/editing the XML. So, once you update this property file, you will find the new keys listed in the combo box:

New acceptable data types in combo box
[New acceptable data types in combo box]

Now, you can specify TEL_US or TEL_FRANCE as the acceptable data type for the request parameter. (Please note that these 2 types were not listed in the combo box of the first example.)

Different input validation for each Locale...

The HTTP request from the CGI FORM (which is shown by the first Servlet) will be sent to the second Servlet, which is responsible for the input validation. Let's look into the source code of the second Servlet:


    private static FormGenerator gen=null;

    public void init()
	throws ServletException{
	try{
	    gen=FormGenerator.getInstance();
	}
	catch(Exception ex){
	    ex.printStackTrace();
	    throw (new ServletException(ex));
	}
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException,IOException{

	try{
	    Locale locale=req.getLocale();
	    Form cgi=gen.getForm("xml/cgi",locale);
	    cgi.setRequest(req);
	    if(!cgi.isAllValid()){
		res.setContentType(cgi.getContentType());
		PrintWriter out=res.getWriter();
		out.print(cgi.getMerged());
		gen.reuseForm(cgi);
		out.flush();
		out.close();
		return;
	    }
	    gen.reuseForm(cgi);
	    Form table=gen.getForm("xml/table",locale);
	    table.setRequest(req);
	    res.setContentType(table.getContentType());
	    PrintWriter out=res.getWriter();
	    out.print(table.getMerged());
	    gen.reuseForm(table);
	    out.flush();
	    out.close();
	}
	catch(Exception ex){
	    throw (new ServletException(ex));
	}
    }


As you notice, there is no difference between the source code in the first example and the one in this example. In this source code, please look the line of:
  • Form cgi=gen.getForm("xml/cgi",locale);
As you learn in the first example, this line of code selects and reads one of the following XML based on the Locale object (the second parameter):
  • (docBase)/WEB-INF/classes/xml/cgi_en.xml
  • (docBase)/WEB-INF/classes/xml/cgi_fr.xml
  • (docBase)/WEB-INF/classes/xml/cgi.xml
Each of these XML includes a set of the localized HTML tags, one for the valid case and another for the invalid case. In addition to a set of the localized HTML tags, these XML also includes the acceptable data types for request parameter from the CGI FORM.
To apply the the different validation algorithm for each Locale, what you need is just to specify the different acceptable data types in the XML for each Locale.

Now, it's time to explore these XML by HtmlToXmlEdit (xmledit.jar). By this tool, you can easily compare the acceptable data type for the telephone number in these XML. (If you are not familiar with this tool, please read its tutorial included in the first example).
Let's start with:
  • (docBase)/WEB-INF/classes/xml/cgi_en.xml
Assuming that you extracted the distribution of this example under:
  • /home/foo
please specify the following files in the main window:
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/classes/xml/cgi_en.xml
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/classes/FormValueVerifierFactory.properties
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/classes/FormValueEscaperFactory.properties
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/classes/FormValueBreakerFactory.properties
Open the XML for English
[Open the XML for English]

When you open the second window, please select Value-6 and click Edit the selection button:

XML for English
[XML for English]

You will see the dialog like below:

Acceptable data type for English
[Acceptable data type for English]

In this dialog, TEL_US is selected for the input validation of the request parameter whose name is tel. As you learn in the previous section, TEL_US is one of the keys listed in:
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/classes/FormValueVerifierFactory.properties
Please close the second window, and let's explore the XML for French. When brought back to the main window, please change the first item to:
  • /home/foo/formgen_ex_2/formgen_2/WEB-INF/classes/xml/cgi_fr.xml
Open the XML for French
[Open the XML for French]

When you open the second window, please select Value-6 and click Edit the selection button:

XML for French
[XML for French]

You will see the dialog like below:

Acceptable data type for French
[Acceptable data type for French]

Now, you will find TEL_FRANCE selected for the input validation of the request parameter whose name is tel.

As you learn in the first example, these XML can easily be generated from the normal HTML by HtmlToXml conversion tool (html2xml.jar). The original HTML are included in:
  • /home/foo/formgen_ex_2/html/en
  • /home/foo/formgen_ex_2/html/fr
It will be the good practice to convert these HTML into the above XML. The step-by-step tutorial of HtmlToXml conversion tool is included in the first example. So, please read it and try the conversion.

Input validation for each country...

Because this is just the example, we make it as simple as possible. But, in case of the web application in the real world, the input validation may be applied for each country, not for each language.
For example, the expected pattern of the telephone number in England may be different than that in U.S.. But, English is spoken in both of these countries. How do you apply the different input validation for the visitors from these countries?

As you know, the Locale object is created from the language preference in the user's web browser. And this language preference consists of:

  • language code
  • country code
The language code is lower-case 2-letter code as defined by ISO-639. You can find a full list of these codes at a number of sites, such as:
The country code is upper-case 2-letter codes as defined by ISO-3166. You can find a full list of these codes at a number of sites, such as: In case of the visitors from U.S., the language preference will be en_US. But, as for the visitors from England, it will be en_GB (Great Britain).
To apply the different input validation for these visitors, what you need is just to prepare the following XML:
  • (docBase)/WEB-INF/classes/xml/cgi_en_US.xml
  • (docBase)/WEB-INF/classes/xml/cgi_en_GB.xml
In each of these XML, you can specify the different acceptable data type for the telephone number. Then, FormGenerator API will select the appropriate XML for each country.

Contact us...

If you have any questions, please feel free to contact us at:

Caution!
All the APIs for Servlet/JSP introduced by this web site are now included in Bento framework:
  • Simpler than JSTL or Apache Struts
  • MVC framework by HTML
  • Input validation from CGI FORM
  • Easy user authentication
  • Easy localization (L10n)
To download the APIs and source code examples, please visit the web site of Bento framework.


JBuilder 2007


General Information

For Java Development

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


ALL CONTENTS COPYRIGHT 1997-2007, OOP-Research Corporation. All rights reserved.