Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

Reusing Content in JSP Pages

There are many mechanisms for reusing JSP content in a JSP page. Three mechanisms that can be categorized as direct reuse--the include directive, preludes and codas, and the jsp:include element--are discussed below. An indirect method of content reuse occurs when a tag file is used to define a custom tag that is used by many Web applications. Tag files are discussed in the section Encapsulating Reusable Content using Tag Files in Chapter 18.

The include directive is processed when the JSP page is translated into a servlet class. The effect of the directive is to 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 as follows:

<%@ include file="filename" %> 

For example, all the Duke's Bookstore application pages could include the file banner.jspf which contains the banner content, with the following directive:

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

Another way to do a static include is with the prelude and coda mechanism described in Defining Implicit Includes. This is the approach used by the Duke's Bookstore application.

Because you must put an include directive in each file that reuses the resource referenced by the directive, this approach has its limitations. Preludes and codas can only be applied to the beginning and end of pages. For a more flexible approach to building pages out of content chunks, see A Template Tag Library.

The jsp:include element is processed when a JSP page is executed. The include action allows you to include either a static or dynamic resource in a JSP file. The results of including static and dynamic resources are quite different. If the resource is static, its content is inserted into the calling JSP file. If the resource is dynamic, the request is sent to the included resource, 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 hello2 application discussed in The examples in this tutorial assume that your application server host and port is localhost:8080. includes the page that generates the response with the following statement:

<jsp:include page="response.jsp"/>  
Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.