The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

Including Content in a JSP Page

There are two mechanisms for including content from another source in a JSP page: the include directive and the jsp:include element.

The include directive is processed when the JSP page is translated into a servlet class. The effect of the directive to the insert the text contained in another file, either static content or another JSP page, in the including JSP page. You would probably use the include directive to include banner content, copyright information, or any chunk of content that you might want to reuse in another page. The syntax for the include directive is:

<%@ include file="filename" %> 

For example, all the bookstore application pages include the file banner.jsp containing the banner content with the following directive:

<%@ include file="banner.jsp" %> 

In addition, the pages bookstore.jsp, bookdetails.jsp, catalog.jsp, and showcart.jsp include JSP elements that create and destroy a database bean with the element:

<%@ include file="initdestroy.jsp" %> 

Because you must statically put an include directive in each file that reuses the resource referenced by the directive, this approach has its limitations. For a more flexible approach to building pages out of content chunks, see A Template Tag Library.

The include element is processed when a JSP page is executed. The include action allows you to include either a static or dynamic file in a JSP file. The results of including static and dynamic files are quite different. If the file is static, its content is inserted into the calling JSP file. If the file is dynamic, the request is sent to the included JSP page, the included page is executed, and then the result is included in the response from the calling JSP page. The syntax for the jsp:include element is:

<jsp:include page="includedPage" /> 

The date application introduced at the beginning of this chapter includes the page that generates the display of the localized date with the following element:

<jsp:include page="date.jsp"/> 
Home
TOC
PREV TOC NEXT Search
Feedback