Java Servlet & JSP Cookbook

Servlets and JSPs often include fragments of information that are common to an organization, such as logos, copyrights, trademarks, or navigation bars. The web application uses the include mechanisms to import the information wherever it is needed, since it is easier to change content in one place then to maintain it in every piece of code where it is used. Some of this information is static and either never or rarely changes, such as an organization's logo. In other cases, the information is more dynamic and changes often and unpredictably, such as a textual greeting that must be localized for each user . In both cases, you want to ensure that the servlet or JSP can evolve independently of its included content, and that the implementation of the servlet or JSP properly updates its included content as necessary.

This chapter recommends recipes for including content in both servlets and JSPs under several conditions:

  • When the included information is refreshed every time a user makes a request.

  • When the included information involves two or more nested levels ”for example, when an included file in turn includes another piece of information, and so on.

  • When you want to use the deployment descriptor to update the item that a servlet includes, which is a handy, less error-prone way of including content when the content is configurable and changes rather often.

  • When you want to import resources into a JSP from outside the web application.

Recipe 6.1 describes how to import a resource each time the servlet handles a request.

Категории