Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Server-side Java: Internationalize JSP-based Websites

Build Websites that speak in tongues

  • Print
  • Feedback

Page 6 of 6

Once the user goes to the checkout counter, it is equally important to get rid of the session object. That is taken care of by having a session.invalidate() invocation at the end of the page. This process is necessary for two reasons. First, if the session is not invalidated, the user's shopping cart is not reinitialized; if the user then attempts to commence another round of shopping upon checkout, his shopping cart will continue to hold items that he has already purchased. The second reason is that if the user simply left the site upon checkout, the session object will not be garbage collected and will continue to take up valuable system resources until its lease period expires. Since the default session-lease period is about 30 minutes, in a high-volume system this can quickly lead to the system's running out of memory.

Listing 8: checkout.jsp



<%@ page import="java.util.*, shopping.i18n.CD" %>
<html>
<head>
<title>
<%=session.getValue("checkout.title")%>
</title>
</head>
<body bgcolor='<%=session.getValue("checkout.bgcolor")%>'>
<font face="Times New Roman,Times" size=+3>
<%=session.getValue("checkout.title")%>
</font>
<p> <hr>
<em><%=session.getValue("checkout.subhead")%></em>
<p>
<center>
<table border="0" cellpadding="0" width="100%"
   bgcolor='<%=session.getValue("cart.bgcolor")%>'>
   <tr>
        <td><%=session.getValue("cd.albumLabel")%></td>
        <td><%=session.getValue("cd.artistLabel")%></td>
        <td><%=session.getValue("cd.countryLabel")%></td>
        <td><%=session.getValue("cd.priceLabel")%></td>
      <td><%=session.getValue("cd.quantityLabel")%></td>
        <td></td>
   </tr>
<%
   Vector buylist=(Vector)session.getValue("shopping.shoppingcart");
     for (int i=0; i< buylist.size();i++) {
         CD anOrder = (CD) buylist.elementAt(i);
%>
         <tr>
            <td><%=anOrder.getAlbum()%></td>
            <td><%=anOrder.getArtist()%></td>
            <td><%=anOrder.getCountry()%></td>
            <td><%=anOrder.getPrice()%></td>
            <td><%=anOrder.getQuantity()%></td>
        </tr>
<%  }  %>
    <tr>
         <td>     </td>
         <td>     </td>
         <td><%=session.getValue("checkout.totalLabel")%></td>
         <td><%=request.getAttribute("amountDue")%></td>
         <td>     </td>
    </tr>
</table>
<p>
<a href=i18nDemo.jsp><%=session.getValue("checkout.returnLabel")%<>/a>
</center>
<%
session.invalidate();
%>
</body>
</html>


Deploying the internationalized Web application

I will assume you are using the latest version of JavaServer Web Development Kit (JSWDK) from Sun for running the example. If not, see Resources to find out where to get it. Assuming the server is installed in \jswdk-1.0.1, its default location in Microsoft Windows, deploy the Music Without Borders application files as follows:

  • Create a i18n directory under \jswdk-1.0.1\examples\jsp
  • Copy i18nDemo.jsp to \jswdk-1.0.1\examples\jsp\i18n
  • Copy eshop.jsp to \jswdk-1.0.1\examples\jsp\i18n
  • Copy cart.jsp to \jswdk-1.0.1\examples\jsp\i18n
  • Copy main.jsp to \jswdk-1.0.1\examples\jsp\i18n
  • Copy checkout.jsp to \jswdk-1.0.1\examples\jsp\i18n
  • Compile CD.java by typing javac CD.java
  • Create a shopping\i18n directory under \jswdk-1.0.1\examples\Web-Inf\jsp\beans
  • Copy CD.class to \jswdk-1.0.1\examples\Web-Inf\jsp\beans\shopping\i18n


Once your server has been started, you should be able to access the application using http://localhost:8080/examples/jsp/i18n/i18nDemo.jsp as the URL.

Speaking of characters

Throughout this example, I made use of the default 8-bit ISO Latin-1 character set (aka ISO-8859-1), since it supports the 200 or so characters common to most Western European languages. But to develop truly international applications, you would need to use a character set capable of supporting languages such as Chinese, Arabic, Japanese, and so forth, whose range of characters far exceeds the 256-character limit of ISO Latin-1. You can obtain a list of character encodings supported by Java from Resources. If you are using something other than the ISO-8859-1 character set, you must communicate this to the browser using the contentType attribute of the page tag, as:

<%@ page contentType="text/html; charset=charset_name" %>


For example, if you generated your dynamic content in Cyrillic (for a page in Russian or Bulgarian, for example), your JSP would have to communicate that fact to the browser as:

<%@ page contentType="text/html; charset=ISO-8859-5" %>


Of course, in this case, your users will also need a browser that supports the new character set. Toward this end, you must ensure that the browsers are HTML 4.0 compliant, since HTML 4.0 supports Basic Multilingual Plane, a standardized 16-bit character set that supports most of the world's languages. Additionally, it is important that the browser is also enabled with the requisite font to display the characters of your target language.

Conclusion

In this article, I have examined the development of multilocale JSP pages. Websites based on JSP technology lend themselves more naturally to internationalization because of the ease with which the presentation view can be separated from application logic. Developing a truly global site is, however, a challenging task, due to the complexities introduced by diverse character sets and erratic browser support.

The authors of this month's server-side Java computing articles will be holding a free online seminar on March 9 at 10:00 a.m. PST. Register to join at http://seminars.jguru.com.

About the author

Govind Seshadri is an enterprise Java guru for jGuru.com, and the author of Enterprise Java Computing -- Applications and Architecture from Cambridge University Press (1999). Learn more about Govind at jGuru.com. JavaWorld and jGuru have formed a partnership to help the community better understand server-side Java technology. Together, JavaWorld and jGuru are jointly producing articles and free educational Web events.

Read more about Enterprise Java in JavaWorld's Enterprise Java section.

  • Print
  • Feedback