Sometimes, there is a need to include a JSP segment from outside the including file's context. Example below shows how to use c:import tag from JSTL tag-library which 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, from another context in the same web container and from the same context, similar to using jsp:include.

Here we include page-segment header.jsp from outside context /sample. The url attribute specifies the resource to include; the context attribute declares the context from which the JSP imports the resource:

<%@page contentType="text/html"%>

////////////// first: include a tag-library by inserting the taglib:

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

////////////// using the c:import tag to import an external URL:


  <c:import url="/header.jsp" context="/sample" />

    <h2><c:out value="${param.title}" /></h2>

    <jsp:useBean id="dateString" class="java.util.Date"/>
    The time is  <c:out value="${dateString}" />.
    <br /><br />



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:

////////////// here is an included external segment:

      <title><%= request.getParameter("title"%> </title>