8.3 Business Object Layer

I l @ ve RuBoard

If you're dealing with servlets, standard JavaBeans or web services, or the name du jour for business object components , you should be certain that you design with internationalization in mind. As far as business objects are concerned , you typically need to know the user 's preferred locale from which to decode and parse information from an incoming request.

8.3.1 Design Internationalization-Ready Business Objects

The components needed to construct your business object layer are too vast to enumerate or give due treatment in this chapter. When designing for internationalization, however, you must always take into consideration the types of data items that you are working with. In this chapter, we cannot go into all the details of how to use the various internationalization APIs for formatting items such as numbers , dates, and times, but we can say that if you haven't done so already, familiarize yourself with the following Java 2 Standard Edition (J2SE) APIs [9] when designing various J2EE components (e.g., servlets, standard JavaBeans, Enterprise JavaBeans (EJBs), and web services). Here are some important classes:

[9] These APIs are present in the Java 2 SDK Standard Edition 1.4.0 API, which can be found online at http://java.sun.com/j2se/1.4/docs/api/index.html.


Allows you to break apart text in a locale-specific way.


Useful if you need to sort a list of data (e.g., strings). The sorting rules will differ per locale.


Appropriate for formatting and parsing date strings in a locale-independent manner.


Used when you need to construct appropriate messages in a language-independent manner.


Appropriate for formatting and parsing numeric values in a locale-independent manner.


As the Java documentation says, "represents a specific geographical, political, or cultural region."


Contains locale-specific objects.

This is by far not an exhaustive list of the APIs you will need to be familiar with, but it touches on the major areas for dealing with data in a J2EE application.

If you look through the various internationalization APIs, a common pattern emerges: constructors (direct or factory-based) and methods all take a java.util.Locale object when the class handles locale-sensitive operations or when the operation being performed is locale-sensitive . As we mentioned in the previous section, at an application-design level, you need to decide where the user's locale preference is stored. Again, this can be a method accessed from the appropriate User object, or it can be an object stored directly in the user's session that is accessed under a certain key.

If you feel as if you're being left in the dark, you're not. As far as the J2EE APIs [10] are concerned, few J2EE APIs ask for a Locale object. [11]

[10] The current Java 2 SDK Enterprise Edition APIs available at the time of this writing are 1.3.1_02. They can be found online at http://java.sun.com/j2ee/sdk_1.3/techdocs/api/index.html.

[11] Actually, we found only 13 methods that explicitly dealt with or returned a Locale object.

Although the dot-com revolution has since peaked, e-commerce is not dead. Therefore, there are a number of areas for you to consider when communicating a locale to the business object layer:

  • Localization of various product prices

  • Currency and tax manipulation

  • External communication via email (e.g., order confirmation)

For certain components, such as the InsertText JSP custom tag developed in Example 8-3, your component should have the appropriate methods for retrieving and setting the locale.

8.3.2 Create a Simple Interface for Supporting Localization in Business Objects

The LocaleSupport interface, defined in Example 8-11, is arguably an oversimplified version of an interface for handling locales. However, certain components might need to retrieve and/or set the locale, but other components might just need to know how to "find" a user's preferred locale by looking for a certain key, which in this case is com.ora.bestpractices.j2ee.i18n.USER_PREFERRED_LOCALE .

Example 8-11. LocaleSupport interface
 package com.ora.i18n;     import java.util.Locale;     public interface LocaleSupport {         static final String USER_PREFERRED_LOCALE = "com.ora.bestpractices.j2ee.i18n.USER_ PREFERRED_LOCALE";     static final Locale APPLICATION_DEFAULT_LOCALE = Locale.getDefault(  );         public void setLocale(Locale inLocale);     public Locale getLocale(  ); } 

The simplicity of this interface underscores the simplicity with which you can start designing your business components to take a locale as appropriate.

8.3.3 Take the Locale into Consideration in the Application Controller

You've already seen LocaleSupport used in a JSP custom tag, but what about a business component such as a servlet? Its use in this scenario might be appropriate if you're designing an application controller to route users to locale-specific JSP pages. As with the LocaleSupport interface, this is not meant to be an exhaustive component, but a starting point for your own development.

The essence of how this business component can use LocaleSupport is as follows :

  • Look for the user's preferred locale attribute from the name defined in the LocaleSupport interface.

  • If the user's preferred locale is not set, fall back to the application's default locale defined in the LocaleSupport interface.

  • Identify the requested page and form an appropriate page name by tacking on the preferred locale to the page and adding the appropriate extension.

  • Forward the user to the proper locale-s

  • pecific JSP page.

Example 8-12 shows one such application controller.

Example 8-12. Servlet controller that helps route to locale-specific JSP pages
 package com.ora.i18n.servlets;     import com.ora.i18n.LocaleSupport;     import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; import java.util.Locale;     public class LocalePageServlet extends HttpServlet {         private final static String DEFAULT_PAGE = "index";     private final static String PAGE_ID_PARAMETER = "pageID";     private final static String JSP_EXTENSION = ".jsp";         public LocalePageServlet(  ) {     }         protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse  httpServletResponse) throws ServletException, IOException {         Locale pageLocale;         pageLocale = (Locale) httpServletRequest.getSession( ).getAttribute(LocaleSupport. USER_PREFERRED_LOCALE);         if (pageLocale =  = null) {             pageLocale = LocaleSupport.APPLICATION_DEFAULT_LOCALE;         }             String requestedPage = httpServletRequest.getParameter(PAGE_ID_PARAMETER);         if (requestedPage =  = null) {             requestedPage = DEFAULT_PAGE;         }         requestedPage += "_";         requestedPage += pageLocale.toString(  );         requestedPage += JSP_EXTENSION;         RequestDispatcher rd = httpServletRequest.getRequestDispatcher(requestedPage);         rd.forward(httpServletRequest, httpServletResponse);     }         protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse  httpServletResponse) throws ServletException, IOException {         doPost(httpServletRequest, httpServletResponse);     } } 

Example 8-13 gives the appropriate definition of the web.xml file.

Example 8-13. Definition of the LocalePageServlet for web.xml in your web application
 <servlet>       <servlet-name>LocalePageServlet</servlet-name>       <servlet-class>com.ora.i18n.servlets.LocalePageServlet</servlet-class>   </servlet>       <servlet-mapping>       <servlet-name>LocalePageServlet</servlet-name>       <url-pattern>/LocalePageServlet</url-pattern> </servlet-mapping> 

In the absence of a more formal or full-scale enterprise application, you use the setlocale.jsp page to set the user's preferred locale preference (see Example 8-14). An appropriate URL for setting the user's preferred locale to Hebrew might be http://localhost:8080/ora/setlocale.jsp?lang=iw . Note that the hostname can differ for your machine.

Example 8-14. setlocale.jsp page used to set the user's preferred locale
 <%@ page import="com.ora.i18n.LocaleSupport,                  java.util.Locale"%> <%@ page session="true" %> <%     String language = request.getParameter("lang");     Locale userLocale = new Locale(language);     session.setAttribute(LocaleSupport.USER_PREFERRED_LOCALE, userLocale); %> <html> The user's locale has been set appropriately. </html> 

In Figure 8-10, you use setlocale.jsp and request the index page. As you can see from the figure, you are served the appropriate page for your preferred locale, Hebrew.

Figure 8-10. Using the LocalePageServlet to request the index page in Hebrew

We included Figure 8-11 to illustrate that the LocalePageServlet is working properly in creating the appropriate JSP page name. However, this particular JSP page does not exist in your application. A more robust controller would handle this situation more gracefully and in a more user-friendly manner .

Figure 8-11. Demonstrating the LocalePageServlet that constructs the JSP page name
I l @ ve RuBoard

The OReilly Java Authors - JavaT Enterprise Best Practices
The OReilly Java Authors - JavaT Enterprise Best Practices
Year: 2002
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net