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

6.6. Using an External Configuration File to Include a Resource in a JSP

Problem

You want to include a file dynamically in a JSP, based on a value derived from a configuration file.

Solution

Use the jsp:include standard action. Provide the value in an external properties file or as a configuration parameter in the deployment descriptor.

Discussion

Using an external configuration to specify an include file for a JSP allows you to change the name and/or path to the included file without touching the JSP's code. In addition, when using jsp:include the JSP does not have to be recompiled to reflect any changes in the included file—the web resource is included by the JSP each time it handles a request. If you change the file pointed to by the configuration file, the response from the included resource is added to the including JSP's response during the next request.

Tip

The difference between a jsp:include standard action and include directive is that the include directive includes the bytes or contents of the imported file before the JSP is compiled (during the translation phase for the JSP). If the included segment changes, the updates will not be reflected in the JSP until the JSP itself is modified, which causes a JSP container (such as Tomcat's Jasper JSP container) to recompile the JSP.

Example 6-14 shows a JSP that uses an external properties file to specify the file to include.

Example 6-14. Using java.util.ResourceBundle.getBundle( ) to fetch an externally configured file

<%@page ...

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