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)
if((country = request.getParameter("country"))==null)
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.
Other user agents send different headers. For example, you can identify iMode (wapprofit.com emulator) user agents with the code:
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,
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.
TransformerFactory tFactory = TransformerFactory.newInstance();
String str = OptionsXML.getOptionsXML(pageType, language,
StringReader sReader = new StringReader(str);
StreamSource xml = new StreamSource(sReader) ;
StreamSource xsl = new StreamSource(xslName) ;
Transformer transformer = tFactory.newTransformer(xsl);
StreamResult result = new