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.4. Including a Resource that Seldom Changes into a JSP

Problem

You want to include a resource that does not change very much (such as a page fragment that represents a header or footer) in a JSP.

Solution

Use the include directive in the including JSP page, and give the included JSP segment a .jspf extension.

Discussion

JSP pages are often composites of page fragments that represent navigation bars, headers (page elements that appear at the top of a web page), footers (elements that appear at the bottom of a web page), and the main body content. Since pages in a web application or a site may all use the same navigation bar, this file is maintained in one place and used by all of the web components that require it. If you are going to import a JSP segment that is a static or unchanging resource, use the include directive in the JSP, as in:

<%@ include file="/WEB-INF/jspf/navbar.jspf" %>

If you are using a JSP document (see Chapter 5) or XML syntax for the JSP, use this form of the include directive:

<jsp:directive.include file="/WEB-INF/jspf/navbar.jspf" />

If the value of the file attribute begins with a "/" character, then it is a context-relative path, meaning that it is relative to the web application containing the JSP that uses this directive. If the JSP includes the latter directive, then this file path means "begin at the web application root and include the /WEB-INF/jspf/navbar.jspf file."

A file attribute value in include that does not begin with a "/" character is a page-relative ...

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