Browse DevX
Sign up for e-mail newsletters from DevX


Building Internationalized J2EE Web Applications for Disparate Clients : Page 3

Learn an extensible architecture for building internationalized Web applications with J2EE that support not only browsers, but also WAP, IMODE, and other client devices.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Structure and Processing in the JSP File
In the sample application, the JSP page is responsible for:

  • Identifying the user agent.
  • Creating the response MIME settings
  • Determining the language in which page is to be generated.
  • Making a request for a generated XML page
  • Applying the appropriate XSLT stylesheet to the generated XML page.
For example, assume a client has made a request for the Options.jsp file (the first page in the sample application). The client's user agent passes the language and country to the server as a parameter (URL parameter). If the JSP page on the server does not receive (or does not recognize) the user agent's language and country settings, the application displays responses in US English. The code fragment below illustrates how a JSP page would retrieve the language and country information from the request.

/* Check for the language in which response should be shown

by default, display response in US English */ if((language = request.getParameter("language"))==null) language="en"; if((country = request.getParameter("country"))==null) country="US"; country = country.toUpperCase();

Based upon the user agent, the JSP page sets the response MIME type, identifies the XSLT stylesheet to be applied to the generated XML, and sets the appropriate pageType value, which the XML generator uses to find the appropriate Resource Bundle files in the XML generator.

In addition, applications should be intelligent enough to identify the appropriate response format. For example, the following code snippet checks for a browser user agent and, if found, performs some specific processing.

String userAgent = request.getHeader("User-Agent"); /* Identify user agent and: 1. Set the pageType variable for XML generation. At that time the application fetches entries from the resource bundle depending upon the language, country and Page type settings. 2. Set the contentType variable. The application sets the MIME header according to the identified user-agent 3. Set the xslName variable. This is the name of the XSLT file used to transform the XML to generate the page. */ if(userAgent.startsWith("Mozilla")) { pageType="Web"; contentType="text/html;charset=UTF-8"; xslName="articles-html-form.xsl"; }

Other user agents send different headers. For example, you can identify iMode (wapprofit.com emulator) user agents with the code:

userAgent.startsWith("Microsoft URL")

And you can identify WAP (Nokia Toolkit) user agents with:


You must use the UTF-8 charset to support I18N. At this point, the application has already identified the user agent and language, so the application can generate an XML page. To initiate XML generation, the JSP makes a call to an XML generator class method, such as the getOptionXML() method of the OptionsXML class, which returns an XML string containing the data for the requested page.

/* calling a private method of JSP that will generate XML and apply an XSL stylesheet to generate output depending upon the user agent */ showPage(pageType, language, country, response, contentType, xslName);

The showPage() method generates the XML page according to the pageType, language, and country settings. It applies the specified XSLT stylesheet to the generated XML and then sends the formatted response back to the user agent.

try{ response.setContentType(contentType); response.getOutputStream().flush(); TransformerFactory tFactory = TransformerFactory.newInstance(); String str = OptionsXML.getOptionsXML(pageType, language, country,response); StringReader sReader = new StringReader(str); StreamSource xml = new StreamSource(sReader) ; StreamSource xsl = new StreamSource(xslName) ; Transformer transformer = tFactory.newTransformer(xsl); StreamResult result = new StreamResult(response.getOutputStream()) ; transformer.transform(xml, result); } catch(Exception e){}

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date