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


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


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


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.

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 contentType="text/html"%> <%@ taglib uri="" prefix="c" %> <html>  <% java.util.ResourceBundle bundle =       java.util.ResourceBundle.getBundle("com.jspservletcookbook.include");        String segment = bundle.getString("external-include");%> <jsp:include page="<%=segment %>"/>  <body> <h2>Welcome to our Portal Home <c:out value="${param.fname}" /> <c:out value="${param. lname}" /></h2> <jsp:useBean id="dateString" class="java.util.Date"/> The time is <c:out value="${dateString}" />.<br><br> </body> </html> 

Example 6-14 includes a JSP segment that is found at the path specified by the external-include property. This property is written in a simple text file called , with content that looks like this:


The file is stored in WEB-INF/classes/com/jspservletcookbook . When your servlet or JSP attempts to access a list of property values by calling the static method java.util.ReseourceBundle.getBundle("com.jspservletcookbook.include") , getBundle automatically replaces the period "." characters with "/" and appends ".properties" to the end of the String (making the search look like "com/jspservletcookbook/" in our example).

The example code saves the property value in a String variable segment with the following code:

 String segment = bundle.getString("external-include"); 

Then the value of the segment variable, which is a filepath, specifies the file for the JSP to include: WEB-INF/jspf/header_tag.jsp . This is accomplished with the JSP expression ” <%=segment %> ”in the page attribute value for jsp:include :

 <jsp:include page="<%=segment %>"/> 

When the JSP page is executed, the included file's response is included in the part of the page where the jsp:include standard action occurs. Example 6-15 shows the content of the included file, header_tag.jsp .

Example 6-15. The content of the header_tag.jsp segment
 <%@ taglib uri="" prefix="c" %> <HEAD>     <META name="author" content=         "Bruce W. Perry,">     <META name="keywords" content=         "Java, JSP, servlets, databases, MySQL, Oracle, web development">     <TITLE>Parker River: Thanks For Visiting        <c:out value="${param.fname}"/>        <c:out value="${param.lname}"/>       </TITLE> </HEAD> 

This is a complete HEAD HTML tag, including two nested META tags and a TITLE tag. If you requested the including JSP page at http://localhost:8080/home/externalInclude.jsp?fname=Mister&lname=Bean , the returned content from this JSP segment ”the actual text that the JSP container substitutes for the jsp:include tag in the output ”looks like Example 6-16.

Example 6-16. The output when jsp:include is used
 <HEAD>     <META name="author" content=         "Bruce W. Perry,">     <META name="keywords" content=         "Java, JSP, servlets, databases, MySQL, Oracle, web development">     <TITLE>Parker River: Thanks For Visiting        Mister        Bean       </TITLE> </HEAD> 

The included segment processes the request parameters fname and lname from the query string:


and includes their values in the TITLE tag. Figure 6-8 shows what the externalInclude.jsp page looks like in a browser.

Figure 6-8. Browser view of JSP that uses jsp:include to include another JSP segment

In this example, the included segment uses the proper taglib directive so that the c:out JSTL 1.0 tags can be used. If you are using JSTL 1.1, then the uri attribute value is :

 <%@ taglib      uri=""       prefix="c" %> 

You can also pass parameters for the included segment to process, in the manner of:

 <jsp:include page="<%=segment %>">   <jsp:param name="role" value="comedian"/> </jsp:include> 

If you want to use a context parameter in the web application's deployment descriptor instead to provide a path for the included file, add a context-param element to web.xml (as shown in Example 6-17).

Example 6-17. A context-param element provides an included file path
 <context-param>     <param-name>external-include</param-name>     <param-value>WEB-INF/jspf/header_tag.jsp</param-value> </context-param> 

Then get the value of the context parameter in the including JSP:

 <jsp:include page="<%=application.getInitParameter("external-include")%>"/> 

The JSP then inserts the file path WEB-INF/jspf/header_tag.jsp as the value for the jsp:include page attribute.

See Also

Recipe 6.4 on the include directive; Recipe 6.7 on including JSP segments in XML files; Recipe 6.8 on including content from outside of a JSP's context; Chapter JSP.1.10.3 of the JSP 2.0 specification on including files in JSPs; Chapter 23 on the JSTL tags; this web page for how the getBundle method returns certain types of ResourceBundles : (java.lang.String, java.util.Locale, java.lang.ClassLoader) .

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: