< Zurück | Inhalt | Weiter >

19.3.4 Directive

The last of the special delimiters that we will discuss is the one that doesn’t di- rectly involve Java code. The <%@ ... %> delimiter encompasses a wide variety of directives to the JSP converter. We don’t have the space or the patience to cover them all, so we’ll cover the few that you are most likely to need early on


Table 19.1 JSP predefined variables


image

Type Variable name


image

image

PageContext HttpSession ServletContext ServletConfig JspWriter

pageContext session application config

out


in your use of JSP. We have some good JSP references at the end of this chapter for those who want all the gory details of this feature.


<%@page import="package.name.*" %>


is the way to provide Java import statements for your JSP. We bet you can guess what that happens in the generated servlet.

Here’s another useful page directive:


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


You’ll see this as the opening line of our JSP, to identify the output MIME type for our servlet.

JSP also has an include directive:


<%@include file="relative path" %>


This directive is, for some applications, worth the price of admission alone. That is, it is such a useful feature that even if they use nothing else, they could use JSP just for this feature. It will include the named file when converting the JSP—that is, at compile time.

It can be used for common header and footer files for a family of Web pages. (If you’re a C programmer, think #include.) By defining one header file and then using this directive in each JSP, you could give all your JSP the same look—say, a corporate logo and title at the top of page and a standard copyright statement and hyperlink to your webmaster’s e-mail address at the bottom.

Be aware that this inclusion happens at compile time and is a source-level inclusion. That is, you are inserting additional source into the JSP, so if your


included file contains snippets of Java code, they will be part of the resulting program. For example, you could define a variable in the included file and reference in the including file.

Also, since this inclusion happens at compile time, if you later change the included file, the change will not become visible until the JSP files that do the including are recompiled. (On Linux, this is simply a matter of touching all the JSP, as in:


$ touch *.jsp


assuming all your JSP files are in that directory. Touching them updates their time of last modification, so the Web server thinks they’ve been modified so the next access will cause them to be reconverted and their generated servlets reloaded. You get the idea.

There is another way to do an include in JSP—one that happens not at compile time, but at runtime. The syntax is different than the directives we’ve seen so far, but more on that in minute. First, an example of this kind of include:


<jsp:include page="URL" flush="true" />


In this format, the page specified by the URL (relative to this Web appli- cation’s root) is visited and its output is included in place amongst the output of this JSP, the one doing the include.

A few quick notes:

• Be sure to include the ending “/” in the directive; it’s part of the XML syntax which is a shorthand for ending the element in the same tag as you begin—that is, <p /> instead of <p> </p>.

• When all is working, flush being true or false doesn’t matter; when the included page has an error, then flush="true" causes the output to the browser to end at the point of the include; with flush="false", the rest of the page will come out despite the error in the include.

• The page that is being included is turned into its own servlet. That is, it is its own JSP. You don’t have to just include static HTML, you can include a JSP.

• Since this is a runtime include, all you are including is the output of that other page. You can’t, with this mechanism, include Java snippets or declarations, but only HTML output.


Table 19.2 New XML syntax for JSP constructs


image

Standard format New HTML format


image

<% code %>

<%! code %>

<%= expr %>

<jsp:scriptlet> code </jsp:scriptlet>

<jsp:declaration> code </jsp:declaration>

<jsp:expression> expr </jsp:expression>


image