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 ...

Get Java Enterprise Best Practices now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.