Chapter 6. Configuration Settings

   

Topics in This Chapter

  • Overview

  • The Config Class

  • Accessing Configuration Settings in Servlets

  • Accessing Configuration Settings in Life-Cycle Listeners

  • Accessing Configuration Settings in Custom Actions

Most of the JSTL actions discussed in the following chapters use configuration settings to create or access resources such as SQL data sources or resource bundles; for example, the following code prints a localized message:

 <fmt:setLocale value='fr-CA' scope='request'/>  <fmt:message key='introduction-page-title'/> 

In the preceding code fragment, the <fmt:setLocale> action assigns a value to a configuration setting known as FMT_LOCALE . [1] The <fmt:message> action uses that configuration setting to retrieve a localized message from an appropriate resource bundle. To effectively use JSTL's internationalization (I18N), formatting, or SQL actions, you must understand how configuration settings work.

[1] See "I18N Actions" on page 248 for more information about the internationalization actions.

You can also separate business and presentation logic by assigning a value to a configuration setting in a business component such as a servlet; for example, you can specify an SQL data source in a servlet, like this: [2]

[2] See "Database Actions" on page 356 for more information about the SQL actions.

 public class InitializationServlet extends HttpServlet {     public void init() throws ServletException {       // Create the data source and store it as a configuration       // variable in application scope  Config.  set(getServletContext(), Config.SQL_DATA_SOURCE,                  new MyDataSource());    }    public void destroy() {  Config.  remove(getServletContext(), Config.SQL_DATA_SOURCE);    } } 

JSTL provides a Config class that implements a number of static methods that let you manipulate configuration settings. In the preceding code fragment, the servlet uses Config.set and Config.remove , respectively, to set the SQL_DATA_SOURCE configuration setting in application scope when the servlet is created and to remove it when the servlet is destroyed .

You can also specify an SQL data source with the <sql:setDataSource> action, but specifying it in a business component, such as the servlet in the preceding code fragment, is often preferable because you have complete control over how your data source is created and because keeping that logic out of JSP pages separates your business logic from your presentation logic.

Because it's important to understand JSTL configuration settings to effectively use the I18N, formatting, and SQL actions and because configuration settings let you separate business and presentation logic, this short chapter discusses configuration settings exclusively. If you're not interested in the I18N, formatting, or SQL actions, you can safely skip this chapter for JSTL 1.0; otherwise , let's enter the fascinating world of JSTL configuration settings.

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

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