O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

24.13. Setting the Localization Context in the Deployment Descriptor

Problem

You want to configure the localization context for the JSTL tags used by a web application.

Solution

Use a context-param element in the application's deployment descriptor.

Discussion

A localization context is a set of resources such as ResourceBundles that your web application's use to provide locale information for a JSP. If the JSP is displaying translated text, it can use JSTL tags (such as fmt:fmtMessage) that detect the user's locale, and then searches within the localization context for the proper text to display.

Set the localization context with a context-param element in web.xml. The parameter name must be javax.servlet.jsp.jstl.fmt.localizationContext. Its value is the fully qualified basename of the ResourceBundle that you have installed in the web application. Example 24-14 shows a context-param element that points to the ResourceBundle we have used throughout this chapter.

Add this type of element to your deployment descriptor as an alternative to using the JSTL tag fmt:setBundle inside JSPs to specify a ResourceBundle.

Example 24-14. Setting the localization context for JSTL tags

<!-- Beginning of web.xml -->

<context-param>

    <param-name>
        javax.servlet.jsp.jstl.fmt.localizationContext
    </param-name>

    <param-value>i18n.WelcomeBundle</param-value>

</context-param>

<!-- Rest of web.xml -->

See Also

Chapter 23 on the JSTL; Recipe 24.2 on detecting the client locale in a JSP; Recipe 24.6 on using a ResourceBundle ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required