Directives are interpreted in page preprocessing. Before the JSP or its resulting servlet is executed, the page directives are processed . Directives look like this:
<%@ someDirective attribute="value" %>
There are three directive types: page , include , and taglib .
Page directives are used to perform certain tasks before the page is loaded. Table 14.1 shows the attributes and accompanying descriptions for the page directive:
Attribute | Description |
---|---|
import | Specify packages to import, just as in Java programming. |
errorPage | Relative URL for the errorPage for this page. |
extends | Java class name that this JSP will extend (rarely used). |
session | Whether this page participates in a session. Possible values are true and false . Default is true . |
language | Scripting language in use on the page. Java is the only currently supported value, though others could be implemented in the future. |
buffer | Indicates the size of the output buffer. Appending kb (for kilobytes) is mandatory. Possible values are none or an integer. |
autoFlush | Whether the buffer will be automatically flushed. Possible values are true or false . A value of true requires the buffer attribute to be set to a valid value. |
isThreadSafe | Whether this page is thread safe or not. Possible values are true or false . |
info | Arbitrary string text describing the page. Can be retrieved in the JSP using servlet.getServletInfo() . |
contentType | Defines the character encoding used in this JSP and the MIME type for the response. |
pageEncoding | Defines the character encoding for the JSP. |
isErrorPage | Set to true if this page is an error page. This makes the exception implicit object available. By default this is false . |
More than one attribute can be specified in a page directive:
<%@ page errorPage="errors/error.jsp" info="A neat page" %>
This directive allows you to include the contents of another file at the specific location where the directive is called. It looks like this:
<%@ include file="some.jsp" %>
Note that this form of including an external file will first run the preprocessor, evaluating the contents of the included file and converting it to a servlet; only then are the contents included. See also the <jsp:include> tag, which does not preprocess the included file. The tag version acts more like a cut-and-paste kind of operation. That is, the <jsp:include> tag is more like the ColdFusion <cfinclude> .
The taglib directive is used to specify the name of a custom tag library you want to import into your JSP. It looks like this:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/ea/core" %>
The above example indicates the taglib directive to use when importing the "Core" libraries from the JSP Standard Tag Library (JSTL). The URI provides a namespace for the library, and the prefix indicates the prefix name you want to use to call tags in the library on this page. So, for instance, once you've used the above taglib directive (and assuming you have the library and have added it to the deployment descriptor), you could do this later in the page:
<c:set var="fileName"> <%= thePath + request.getParameter("file") + ".doc" %> </c:set>
We will discuss tags in the JSTL and tag libraries in general later. The above tag just sets a variable called fileName to whatever string the expression evaluates to.
Top |