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

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