Recipe 6.8 Including Content from Outside a Context in a JSP


Problem

You want to include a JSP segment from outside the including file's context.

Solution

Use the c:import JSTL core tag.

Discussion

The 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:

  • From outside JSP's web container, using an absolute URL (such as http://java.sun.com/api) .

  • From another context in the same web container. For example, your domain may include a central repository of included content at http://www.mydomain.com/warehouse . A JSP page that is installed in a context named /customer could import a resource from the /warehouse context by using: <c:import url= " /catalog_header.jspf" context= " /warehouse" />

  • From the same context, similar to using jsp:include .

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... --> 

Using Tomcat, the context element in conf/server.xml has to include this attribute/value pair or the JSP that uses c:import will raise an exception if it attempts to import resources from another context:

 crossContext="true" <!--"false" by default--> 

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" />  

If a JSP uses c:import to access a forbidden resource (which will cause the receiving server to respond with a HTTP status code 403), the c:import tag throw ans exception and the JSP compilation will fail.


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 Also

Recipe 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.



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

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net