OOP-ResearchMake It Simpler by Object Oriented Programming

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

Source code example for input validation from CGI FORM by XML. Simpler than JSP/JSTL, Apache Struts or JavaServer Faces. Localization (L10n) in Servlet. Japanese Shift_JIS, Chinese BIG5 or GB2312.
This is the first-step example of OOP FormGenerator API. In this source code example, all the response from Java Servlet (web application GUI, or View part of MVC (Model View Controller)) can be generated from the normal HTML (or any mark up language such as XHTML, WML and HDML). Unlike the JSP/JSTL based solution (such as Apache Struts or JavaServer Faces), all the response can be written in just the HTML tags. The web designers (or page authors) need not learn even a bit of custom tag libraries in JSP/JSTL.
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. Given this XML, a few lines of codes will be enough for the input validation in your Servlet.
In case of the JSP/JSTL based solution like Apache Struts or JavaServer Faces, the localized messages should be written in the property resource files for each expected Locale. But in this framework design, the web designers (or page authors) can write the localized messages into the normal HTML (or any mark up language such as XHTML, WML and HDML) in as many human languages as you like. Based on the Locale object (language preference from the web browser), the localized message will be selected automatically. By this way, the localization (L10n) of your Servlet can be much easier than the JSP/JSTL based solution (such as Apache Struts or JavaServer Faces). In addition, there is not difficulty even in the 2 bytes characters such as Japanese Shift_JIS, Chinese BIG5 or GB2312.

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_1/formgen_1 This is docBase directory of this example Servlet. Please install this directory into your Tomcat, Jetty or any other JSP/Servlet server.
formgen_ex_1/formgen_1
  • /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_1/formgen_1
  • /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_1/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_1/formgen_1/WEB-INF/classes
But, these source code will be the good starting point of your Servlet programming with this Java API.
formgen_ex_1/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_1
  • /home/foo/formgen_ex_1/formgen_1
  • /home/foo/formgen_ex_1/formgen_1/WEB-INF/lib
  • /home/foo/formgen_ex_1/formgen_1/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_1/formgen_1/WEB-INF/lib
Please edit:
  • /home/foo/formgen_ex_1/formgen_1/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_1/formgen_1
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_1" 
      docBase="/home/foo/formgen_ex_1/formgen_1" 
      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_1/index.html
or the similar one.

Input validation from CGI FORM...

This source code example consists of 2 Servlet:

  • example.formgen1.Cgi
  • example.formgen1.Verify
The first one generates the empty CGI FORM.
CGI FORM
[Empty CGI FORM]

The POST request from this CGI FORM will be sent to the second Servlet. The second Servlet verifies all the request parameters. If some request parameter is invalid, the second Servlet shows the same CGI FORM again. But, at this time, the CGI FORM includes the previous request parameters in it, as the default values. In addition, this CGI FORM indicates which input is invalid.
CGI FORM with invalid input
[CGI FORM with invalid input]

The POST request from this CGI FORM will be sent to this Servlet again. When all the request parameters are verified, the second Servlet shows the HTML table including them.
Table with valid input
[HTML table with valid input]

In this example, all these responses are generated from the XML. The XML includes a set of the HTML tags, and the Servlet merges them into the concatenated HTML tag for the response. Some XML also includes the acceptable data type for each request parameter, by which the Servlet verifies the request parameters from the CGI FORM.

Empty CGI FORM by the first Servlet...

Let's look into the source code of the first 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 doGet(HttpServletRequest req, HttpServletResponse res)
	throws ServletException,IOException{
	try{
	    Locale locale=req.getLocale();
	    Form cgi=gen.getForm("xml/cgi",locale);
	    res.setContentType(cgi.getContentType());
	    PrintWriter out=res.getWriter();
	    out.print(cgi.getMerged());
	    gen.reuseForm(cgi);
	    out.flush();
	    out.close();
	}
	catch(Exception ex){
	    throw (new ServletException(ex));
	}
    }



Among the source code above, please look the line of:
  • Form cgi=gen.getForm("xml/cgi",locale);
Based on the Locale object specified as the second parameter, this line of code reads the XML from:
  • (docBase)/WEB-INF/classes/xml
In case of the English Locale object specified,
  • (docBase)/WEB-INF/classes/xml/cgi_en.xml
will be selected. This XML includes the HTML tags in English language. If the Locale object for French is specified, the following XML will be selected instead:
  • (docBase)/WEB-INF/classes/xml/cgi_fr.xml
As you guess, this XML includes the HTML tags in French language.
Based on the language preference of the user's web browser, Locale object is created by Servlet/JSP server at:
  • Locale locale=req.getLocale();
By this way, the Servlets in this example can show the localized response, which is suitable for the user's language preference. You can change the language preference of your web browser:

Language Preference on web browser
[Language Preference on web browser]

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. So, please change your language preference to English/French and try this example. For each language preference, you will see the different response, one in English and another for French.

Because you cannot prepare the XML for all the possible Locale in the world, the default XML must always be prepared. If the XML for the expected language is not found, the default XML will be selected. For example, if the preferred language is Japanese but the XML for Japanese is not found, the default XML will be selected. In this example, the default XML are:
  • (docBase)/WEB-INF/classes/xml/cgi.xml
In usual, the default XML are written in English language, because US-ASCII characters can be rendered on any web browsers.

By this way, the first Servlet can read a set of the localized HTML tags from the XML. In this example, all of the following XML include a set of the HTML tags for the CGI FORM:
  • (docBase)/WEB-INF/classes/xml/cgi_en.xml
  • (docBase)/WEB-INF/classes/xml/cgi_fr.xml
  • (docBase)/WEB-INF/classes/xml/cgi.xml
You may want to explore these XML, but please be patient here.
Because the first Servlet should show the empty CGI FORM, what you want here is just the concatenated HTML tags, i.e. you need not insert the additional Strings between the HTML tags within XML. Please look into the line below:
  • out.print(cgi.getMerged());
The method of getMerged( ) returns the concatenated HTML tags, and this is what you want. And it will be sent back to the web browser as the response.

Input validation by the second Servlet...

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));
	}
    }


Among the source code above, please look the line of:
  • Form cgi=gen.getForm("xml/cgi",locale);
As described in the previous section, the XML will be selected and read from:
  • (docBase)/WEB-INF/classes/xml
based on the Locale object specified by the second parameter. In case of the English Locale object specified,
  • (docBase)/WEB-INF/classes/xml/cgi_en.xml
will be selected. This XML includes the HTML tags in English language. With the Locale object for French is specified,
  • (docBase)/WEB-INF/classes/xml/cgi_fr.xml
will be selected instead. As you guess, this XML include the HTML tags in French language.

In this example, one of the following XML will be selected:
  • (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. Again, you may eager to explore the structure of these XML. But, please be patient here.

Now, please look into the next line. It is:
  • cgi.setRequest(req);
This method sets all the request parameters into Form object. Because Form object is created from the above XML (which includes the acceptable data types for request parameters), the input validation from the CGI FORM can be done just by:
  • if(!cgi.isAllValid( ))
If some of the request parameters are invalid, the same CGI FORM should be shown again to ask the user to fix the wrong input. But in this case, the CGI FORM should include all the previous request parameters as the default values. In addition, the wrong input should be indicated in the CGI FORM.
So, what you need here is the concatenated HTML tag for the CGI FORM along with the previous request parameters in it. And, to indicate the invalid request parameter, the HTML tags for the invalid case should be selected.
You can get what you want just by:
  • cgi.getMerged( )
This method:
  • Selects a set of the HTML tags, based on the input validation
  • Inserts the request parameters between the selected HTML tags
  • Concatenates the selected HTML tags and the request parameters
  • Returns the concatenated HTML tag
In case of all the valid request parameters, this Servlet shows them in the HTML table. To generate this HTML table, another set of XML is included in this example. They are:
  • (docBase)/WEB-INF/classes/xml/table_en.xml
  • (docBase)/WEB-INF/classes/xml/table_fr.xml
  • (docBase)/WEB-INF/classes/xml/table.xml
Also here, the XML for the localized HTML tags will be selected by:
  • Form table=gen.getForm("xml/table",locale);
To insert all the request parameters between the HTML tags, what you need is only the single line of code. It is:
  • table.setRequest(req);
After that, you can get the concatenated HTML tag just by:
  • table.getMerged( )
The returned String is the HTML tags with the HTML table, which lists the request parameters from the CGI FORM.

How to generate the XML...

As you seen above, the source code of this example Servlet is very simple. All the HTML tags can be written into the XML, along with the acceptable data type for each request parameter. And, based on the Locale object by the language preference, FormGenerator API selects/reads the appropriate XML. As a result, a few lines of code are enough for:

  • input validation of the request parameter from the CGI FORM
  • Generating the dynamic and localized response
Thus, the XML plays an important role in this example. So, you may want to explore the XML under:
  • formgen_ex_1/formgen_1/WEB-INF/classes/xml
These XML look somewhat cryptic due to the many escaped characters, and it may be difficult to grasp their structure.
But, don't worry. The distribution FormGenerator API includes HtmlToXmlEdit (xmledit.jar):

HtmlToXmlEdit
[HtmlToXmlEdit]

By this tool, you can easily explore/edit these XML. Please read through the tutorial for this tool. Once you grasp the structure of the XML, you may wonder how you can write such a cryptic XML. Believe or not, all the XML in:
  • formgen_ex_1/formgen_1/WEB-INF/classes/xml
are generated from the HTML in:
  • formgen_ex_1/html/en
  • formgen_ex_1/html/fr
Yes, what you need is just a normal HTML! (Any mark-up languages, such as XHTML, WML or HDML, are also acceptable.) Then, HtmlToXml conversion tool (html2xml.jar) takes care of you:

HtmlToXml
[HtmlToXml]

This HTML-to-XML conversion tool is included in the distribution of FormGenerator API.
Let's look the source code below (quoted from the above) again:


	    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);


Based on the specified Locale object, the first line selects/reads one of:
  • (docBase)/WEB-INF/classes/xml/cgi_en.xml
  • (docBase)/WEB-INF/classes/xml/cgi_fr.xml
  • (docBase)/WEB-INF/classes/xml/cgi.xml
And the selected XML is used for:
  • input validation of the request parameter from the CGI FORM
  • Generating the dynamic and localized response
To generate this kind of XML, HtmlToXml conversion tool reads a pair of the normal HTML, one for the valid case and another for the invalid case. While in the conversion, this tool asks you to specify the acceptable data type for each request parameter. Please read through the tutorial of HtmlToXml at the link below: Let's look into another fragment of the source code:


	    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();


The first line selects one of
  • (docBase)/WEB-INF/classes/xml/table_en.xml
  • (docBase)/WEB-INF/classes/xml/table_fr.xml
  • (docBase)/WEB-INF/classes/xml/table.xml
And the selected XML is used only for:
  • Generating the dynamic and localized response
Yes, these XML are not used for the input validation. Rather, they are used as the template for the dynamic response. To generate this kind of XML, HtmlToXml conversion tool reads just a single HTML, because there is no need for the invalid case. Please read through another tutorial of HtmlToXml at the link below: Now, you learned how to generate/edit the XML by the tools in this Java API. It will be the good practice to convert the XML under:
  • formgen_ex_1/html/en
  • formgen_ex_1/html/fr
into the XML. Or, please prepare the HTML in another language and convert it to the XML.

Localized message for each country...

You may want to prepare the localized message also for each country, not only for each language. For example, the message to the visitors from U.S. may be different than that to the visitors from England, while both messages are written in English language.
As described above, 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.
To show the different message for these visitors, what you need is to prepare the XML like:
  • (docBase)/WEB-INF/classes/xml/table_en_US.xml
  • (docBase)/WEB-INF/classes/xml/table_en_GB.xml
  • (docBase)/WEB-INF/classes/xml/table.xml
Then, FormGenerator API will select the appropriate XML for each country.

Next step...

By this source code example, you learned:

  • How to generate the XML for the input validation
  • How to generate the template XML for the dynamic response
  • How to use these XML in Servlet
As you seen, Localization (L10n) can be done very easily, because the XML for the intended language can be selected automatically. But, if your CGI FORM includes the text input for the telephone number, how can you verify the request parameter for it? The expected pattern of the telephone number in U.S. differs with the one in France.
Don't worry. Only the slight modification is enough for this case. (Believe or not, you need not touch the source code of the Servlet!)
In the next example, you will learn about the input validation based on Locale object, i.e. the different input validation can be done for each Locale.

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.