Welcome to Sun ONE Web Server i18n technology samples

Overview

The i18n technology samples are collection of servlets and jsps used to demonstrate the i18n capabilities of Sun ONE Web Server. The samples cover some of the most frequently used features of Java Servlet and Java Server Pages Specification Implementation in Sun ONE Web Server.
Servlet 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".

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
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 "ant 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.
Simple Filter
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 "ant all" from the directory "src" and restart your server instance.

How Servlet Request decoding works

Character encoding of the request | Priority
--- | ---
SetCharacterEncoding() | 1
Hidden field | 2
default-charset | 3
Default (ISO-8859-1) | 4

If the application does not set request encoding using setCharacterEncoding(), the web server looks for the hidden field in the request header. If there is any value specified, the web server uses the hidden value to call setCharacterEncoding() method on servlet request. If the hidden field is not available, the web server looks for default-charset value if mentioned in the parameter-encoding element of sun-web.xml. web server uses the corresponding encoding to call setCharacterEncoding() on servlet request. If there is no default-charset defined in sun-web.xml file, then the web 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) 2002 Sun Microsystems, Inc. All rights reserved.