JSP directives do not display anything. However directives are used to change the functionality of a JSP page. They are used to pass a message to JSP container. Directives begin with <%@ and end with %>. There are three JSP directives –
The page directive can take many different attributes. One page directive element can have multiple attributes. The list of these attributes is included below –
- info attribute
- language attribute
- contentType attribute
- pageEncoding attribute
- extends attribute
- import attribute
- session attribute
- buffer attribute
- autoflush attribute
- errorPage attribute
- isThreadSage attribute
- isELIgnored attribute
The info attribute of page directive is used to provide an informational message about the JSP page. The JSP container ignores the info attribute. The info attribute is meant for JSP programmers to help them understand about the functionality of the page.
|<%@page info=”JSP tutorial by Javaprepare.”%>|
The equivalent code in XML-compatible syntax is –
|<jsp:directive.page info=”JSP tutorial by Javaprepare.”/>|
JSP containers may support languages other than Java. However most popular JSP container support only Java. The language attribute of page directive is used to specify the scripting language. The syntax for the language attribute is illustrated below –
The language attribute is set to java by default. So most JSP programs do not specify the language attribute of page directive.
The contentType attribute of page directive is used to specify the format of the document that JSP will generate. Example below specifies that the generated file is of HTML format.
The three commonly used values of contentType are –
The pageEncoding attribute of page directive is used to specify the character set of the generated file. The default character set is ISO-8859-1. Example below illustrates the use of pageEncoding attribute of page directive.
JSP pages are converted into servlets behind the scenes by JSP container. The extends attribute of page directive lets you specify the base class for the servlet that JSP container generates for the JSP page. The example below illustrates the use of extends attribute of page directive.
extends attribute usage is very rare. JSP containers default behavior almost always suffices.
The import attribute of page directive has the same purpose as the Java import statement. If the page uses a Java class, then its complete path (including the package hierarchy) can be specified with the import directive.
square root of 25 is <%Math.sqrt(25); %>
In the above example JSP container knows the path of the Math class because of the import statement. Hence when the sqrt method is invoked, complete class name of the Math class need not be specified. import attribute is the only attribute of page directive which can take multiple values. For all other attributes of the page directive, at most one value can be specified in the JSP file. So the following is legal JSP.
All classes of the following four packages are imported by default into JSP pages –
No import statement is required in JSP files for classes in the above packages.
JSP container provides in-built session management. If the JSP page does not need session management, then the session attribute can be set to false. The syntax of the session attribute of page directive is included below:
By default, session attribute is set to true, i.e. Session management is enabled by default. If session attribute is set to false, the session implicit variable is not available to the JSP page.
The buffer attribute of page directive is used to specify behavior of JSP container with respect to sending the output to an intermediate buffer, before it is sent to Http response. The default size of buffer is 8KB. This means that the JSP container will send the HTTP response in chunks of 8 KBytes.
The feature can be disabled by setting the buffer attribute to none. In this case, JSP container does not perform any buffering, and sends contents to the client as they are generated.
The buffer attribute of page directive may also be used to change the buffer size. The example below sets the buffer size to 4 KBytes.
The autoflush attribute of page directive is used to specify JSP containers behavior when the buffer gets full. autoflush attribute takes boolean values true or false as input. The default value for autoflush is true. The default behavior is that after the buffer is full, the page’s output is sent as response. If autoflush is set to false, JSP container will raise an exception if the buffer gets full. If autoflush is false, then an error page gets generated, when the buffer gets filled.
If buffer is set to none, autoflush cannot be set to false.
The errorPage attribute of page directive defines the behavior of the application if the JSP container gets any error when processing a page. The errorPage attributes is set to the URL of a the page. If the JSP page generates an exception, control is transferred to the URL specified by errorPage. The example below illustrates the usage of autoflush attribute of page directive.
The isErrorPage attribute of page directive is used to specify that the page may be used as an error page. It takes boolean value true or false. The default value of isErrorPage is false. The example below sets the error page to error.jsp for current page.
The exception implicit object is available to a JSP page, only if isErrorPage is set to true.
The isThreadSafe attribute of page directive informs the JSP container how the JSP page should behave if multiple requests are received at the same time. It takes boolean values true or false. If isThreadSafe attribute is set to true, then multiple request received for the JSP page are handled simultaneously. The default value of isThreadSafe attribute is true. If the isThreadSafe attribute is set to false, then requests to the JSP page are processed sequentially. The example below illustrates the usage of isThreadSafe attribute of page directive.
The isELIgnored attribute takes boolean value of true or false as input. If isELIgnored is true, then any Expression Language in JSP is ignored. The default value of isELIgnored is false. The example below illustrates the usage of isELIgnored attribute of page directive.
1. Online Compilers - Compile & Run your C/C++ programs:
2. Ask your questions or clarify your/others doubts from,