Overview
The i18n technology samples are collection of servlets and jsps used to demonstrate the i18n capabilities of the Application Server. The samples cover some of the most frequently used features of Java Servlet and Java Server Pages Specification Implementation in the Application Server.
Servlet
2.3 Samples
Servlet
2.4 Samples
The servlet samples are collection of simple servlets that provide details such as Formatting dates, including a servlet, forwarding request to another servlet, sending localized error messages, setting character encoding to interpret the request, internationalizing servlets using resource bundles and forwarding request to another resource such as jsp.
Internationalizing
JSP with Resource Bundle
The simple ResourceJsp.jsp uses two resource
bundles (LocalStrings.properties and LocalStrings_ja.properties) and loads
appropriate bundle based on the browser's language settings. To verify
that the LocalStrings_ja.properties file is loading properly, change your
browser's language settings to "ja". Click
here to run this sample. If you want to try with any other resource
bundle say "zh", copy LocalStrings_ja.properties file to LocalStrings_zh.properties
and add chinese (unicode data) to LocalStrings_zh.properties file. Change
your browser's language settings to "zh".
Internationalizing JSP using JSTL1.1
Instead of creating your own tag library shown above for resource bundle in jsp, you can
simply use JSTL's fmt class. This example demonstrates how easily you can take advantage of
resource bundle and data formating defined in JSTL1.1 in order to internationalize
your jsp. The bundle name is defined in web.xml in this sample; however, you can also
define the bundle name inside jsp using <fmt:bundle>, especially, when you're using
more than one resource bundle files.
Using
Java Bean with jsp
The simple UseBeanJsp.jsp uses the usebean
property to set and get the values from a Java Bean. Click
here to run this sample. On the screen displayed, enter your name and
greeting message (in desired language) and click Submit. The jsp displays
the entered information.
Simple
Custom Tag
Simple
Filter
The simple TaglibJsp uses
a tag library i18ntaglib to display localized messages from resource bundles
to the user. For this sample we are using LocalStrings_ja.properties resource
bundle and the language is set to "ja". If you want to try with any other
resource bundle say "zh", copy LocalStrings_ja.properties file to LocalStrings_zh.properties
and add chinese (unicode data) to LocalStrings_zh.properties file. In the
TaglibJsp.jsp file change the line:
<i18n:message name="LocalStrings"
key="thanks" language="ja" country="JP" /> to
<i18n:message name="LocalStrings"
key="thanks" language="zh" country="CN" />
Once you are done with
the changes, run "asant all" from the directory "src" and restart your
server instance. Change your browser's language setting to "zh" and verify
that the resource bundle is loaded properly and the chinese characters
displayed properly.
A Filter is a component that is invoked
by the servlet container when a client requests a resource that the Filter
is configured to, such as a Servlet or URL. In this example we use a simple
filter called SimpleFilter, that sets the request's character encoding
based on two initialization parameters, usefilter and encoding defined
in web.xml file. The parameter "usefilter" value can be set either to "true"
or "false". The parameter "encoding" can be set to any valid encoding.
In this example, we have set it to "UTF-8". Depending on this value, the
filter sets the character encoding accordingly. Though the filters can
be used for all the requests received by the container, in this example
we will use the SimpleFilter to serve requests for a specific servlet resource,
SimpleFilterServlet. Once you click on the above link (Set character encoding
using a Filter), a html page will be displayed whose content type is set
to UTF-8. Enter some multi byte data into the form and click Submit. The
resulting page displays the data you have entered. If you want to try with
any other encoding, for example GB2312, modify the line in web.xml UTF-8
to GB2312. And modify the line in servletfilter.html
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html;
charset=UTF-8"> with
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html;
charset=GB2312">. Once you are done with the changes, run "asant all" from
the directory "src" and restart your server instance.
Locale
charset mapping
How it works
The Application Server's web container
provides a locale-charset mapping to set the request's character encoding
if there is no encoding specified to interpret the request. The mapping
definition is available in sun-web.xml file. This simple servlet reads
the request parameter using the encoding specified in the locale-charset
mapping table.
The locale-charset mapping information
provided in sun-web.xml file is used by the application server to set request's
character encoding if the application does not call setCharacterEncoding()
on request object. Here is the priority list for different approaches used
by the application server.
Character encoding of the request | Priority |
---|---|
SetCharacterEncoding() |
|
Hidden field |
|
Locale-charset info |
|
Default (ISO-8859-1) |
|
If the application does not set request
encoding using setCharacterEncoding(), the application server looks for
the hidden field in the request header. If there is any value specified,
the application server uses the hidden value to call setCharacterEncoding()
method on servlet request. If the hidden field is not available, the application
server looks at the language header to get the locale of the request. If
the request locale matches with the the locale defined in locale-charset
mapping table, the application server uses the corresponding encoding to
call setCharacterEncoding() on servlet request. If there is no locale available
in the request header, the application server uses the encoding associated
with the default locale defined in locale-charset mapping table. If there
is no default locale defined in locale-charset mapping table of sun-web.xml
file, then the application server uses ISO-8859-1 as default encoding to
read the request parameters.
Trouble
Shooting
If you use the samples with japanese characters
using Netscape Communicator 4.x browser on Windows NT/2000, you may see
the characters as square boxes or corrupted instead of Japanese characters.
In this case, you will need to install unicode font or Japanese font on
your system.
Download and install
Bitstream Cyberbit font:
Download bitstream cyberbit font (geottf.zip)
from http://www.will-harris.com/fonts/freefonts.htm
or any other web site, to a temp directory. Use an utility such as winzip
to decompress the zip file. To install the font, double click "my computer"
--> "control panel" --> "fonts". Select File -->; Install new font and
select the font file from the temp directory to install.
Setup for Netscape Communicator
Start the browser. Select Menu --> Edit
--> Preferences. Click Appearance --> Fonts. Select "Japanese" for "Language
for encoding". Select "Bitstream Cyberbit" for variable and fixed width
fonts
Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved.