ProblemYou want to include a file dynamically in a JSP, based on a value derived from a configuration file. SolutionUse the jsp:include standard action. Provide the value in an external properties file or as a configuration parameter in the deployment descriptor. DiscussionUsing 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.
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="http://java.sun.com/jstl/core" 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 include.properties , with content that looks like this: external-include=WEB-INF/jspf/header_tag.jsp
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="http://java.sun.com/jstl/core" prefix="c" %> <HEAD> <META name="author" content= "Bruce W. Perry, author@jspservletcookbook.com"> <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, author@jspservletcookbook.com"> <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: fname=Mister&lname=Bean 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
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 AlsoRecipe 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 : http://java.sun.com/j2se/1.4.1/docs/api/java/util/ResourceBundle.html#getBundle (java.lang.String, java.util.Locale, java.lang.ClassLoader) . |