How to Use Page Directives in Java - dummies

By Doug Lowe

A page directive is a JSP element that sets options that determine how the JSP is converted to a servlet. The basic format of a page directive in Java is this:

<%@ page attribute=value %>

The attribute can be any of the attributes listed in this table. (A few attributes besides these are available, but they’re rarely used.)

Commonly Used Page Directive Attributes
Name Description
import=“package.class” Adds an import statement to the servlet so that you can use classes in other JSP elements without having to qualify them fully.
content-Type=“MIME-type” Lets you specify the type of document created by the servlet. The default is text/html. You rarely need to change this setting.
isThreadSafe=“boolean” If true, the servlet is assumed to be thread-safe. If false, implements SingleThreadModel is added to the servlet class declaration so that the thread runs in the single-thread model. The default is true.
session=“boolean” If true, the servlet uses session management. The default is true.
buffer=“size” Specifies the size of the buffer used by the out variable. The default depends on the server but is never smaller than 8KB.
errorPage=“URL” Specifies the name of an error page that is displayed if this servlet throws an uncaught exception.
isErrorPage=“boolean” If true, this page is an error page for some other JSP page. The default is false.

The page directive you use most is import, as it lets you import the packages for API classes so that you can use them in expression, scriptlet, and declaration elements. Here’s a page directive that imports the java.util package:

<%@ page import=“java.util.*” %>

You can place page directives anywhere you want in a JSP document, but you might want to place them at or near the top so they will be easy to find.