ProblemYou want to include a JSP segment from outside the including file's context. SolutionUse the c:import JSTL core tag. DiscussionThe c:import tag gives JSP page authors much flexibility in pulling in resources from inside and outside their web application. The c:import tag allows a page to import web resources:
This recipe includes examples of importing resources from outside the importing JSP's context. Example 6-19 imports a JSP segment header_tag.jsp from the /dbproj context. The url attribute specifies the resource to include; the context attribute declares the context from which the JSP imports the resource. To use the c:import tag, the JSP has to include a taglib directive such as: <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> Example 6-20 includes a group of taglibs by inserting the taglib-inc.jspf JSP segment in the second line. Example 6-20. Using the c:import tag to import an external URL <%@page contentType="text/html"%> <%@ include file="/WEB-INF/jspf/taglib-inc.jspf" %> <html> <c:import url="/header_tag.jspf" context="/dbproj" /> <body> <h2>Welcome to our Portal <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> The c:import tag inserts the text generated by /dbproj/header_tag.jsp in the part of the code where the c:import tag is located. The /dbproj context path represents a different web application or context than the importing JSP. The top of the importing page now looks like the following text, since this is the HTML that the imported file produces: <html> <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> <body> <!-- page continues from here... -->
Example 6-21 imports a description of the HTTP/1.1 protocol, Request For Comments (RFC) 2068. The example declares its content type as "text/plain," so that the browser does not try to display the text file as HTML, which can be unreadable with plaintext files. Then Example 6-21 uses a taglib directive so that the JSP can use the c:import tag. The c:import tag specifies the location of the imported text file as an absolute URL: http://www.ietf.org/rfc/rfc2068.txt. Example 6-21. Using c:import to import a text resource whose address is an absolute URL <%@page contentType="text/plain"%> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <c:import url="http://www.ietf.org/rfc/rfc2068.txt" />
You can also include parameters with c:import using nested c:param tags. Example 6-22 imports a file header_tag.jspf , and makes available two request parameters for that file to process: fname and lname . The taglib directive at the top of Example 6-22 allows the use of the c:import and c:param tags later on in the code. Example 6-22. Including parameter values using c:param <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <c:import url="WEB-INF/jspf/header_tag.jspf" > <c:param name="fname" value="Mister"/> <c:param name="lname" value="Bean"/> </c:import> <body> <h2>The rest of the page goes here ...</h2> </body> </html> The header_tag.jspf file takes the values of the two parameters and adds them to the TITLE tag's greeting. Example 6-23 shows the HTML that results from this import action. Example 6-23. Request parameter values are reflected in the HTML output<html> <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> <body> <h2>The rest of the page goes here ...</h2> </body> </html> See AlsoRecipe 6.1-Recipe 6.3 on including resources in servlets; Recipe 6.4-Recipe 6.7 on using jsp:include , the include directive, and including resources in JSP documents or XML files; Chapter 23 on Using the JSTL 1.0; Chapter JSP.5.4 of the JSP 2.0 specificationon jsp:include ; Chapter JSP.1.10.3 of the JSP 2.0 specification on the include directive. |