Example & Tutorial understanding programming in easy ways.

What is page directive in jsp?.

The page directive can take many different attributes. One page directive element can have multiple attributes.

The list of these attributes is given below..

a)import attribute            b)session attribute          c)buffer attribute
d)autoflush attribute        e)errorPage attribute        f)isThreadSage attribute
g)isELIgnored attribute     h)info attribute                i)language attribute
j)contentType attribute     k)pageEncoding attribute   l)extends attribute

a)import attribute in page directive:

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.
<%@page import="java.lang.Math"%>
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.

<%@page import="java.util.List"%>

<%@page import="java.util.ArrayList"%>

All classes of the following four packages are imported by default into JSP pages :

java.lang

javax.servlet

javax.servlet.http

javax.servlet.jsp

No import statement is required in JSP files for classes in the above packages.

b)session attribute in page directive:

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:
<%@page session="false"%>
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.
 


c)buffer attribute in page directive:

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.

<%@page buffer="none"%>
The buffer attribute of page directive may also be used to change the buffer size. The example below sets the buffer size to 5 KBytes.

<%@page buffer="5kb"%>

d)autoflush attribute in page directive:

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.
<%@page autoflush="false"%>
If buffer is set to none, autoflush cannot be set to false.

e)i)ErrorPage attribute in page directive:

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.

<%@page errorPage="./error.jsp"%>

ii)isErrorPage attribute in 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.

<%@page isErrorPage="true"%>

The exception implicit object is available to a JSP page, only if isErrorPage is set to true.

f)isThreadSafe attribute in page directive:

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.

<%@page isThreadSafe="true"%>

g)isELIgnored attribute in 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.

<%@page isELIgnored="true"%>

h)Info attribute in page directive:

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 "%>

The equivalent code in XML-compatible syntax is -

="JSP tutorial "/>

i)Language attribute in page directive:

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 -
<%@page language="java"%>
The language attribute is set to java by default. So most JSP programs do not specify the language attribute of page directive.

j)contentType attribute in 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.

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

The three commonly used values of contentType are -

contentType Generated file type

text/HTML HTML file

text/xml XML file

text/plain text file

k)pageEncoding attribute in page directive:

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.
<%@page pageEncoding="ISO-8859-1"%>

l)extends attribute in 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.
<%@page extends="myServletClass"%>
extends attribute usage is very rare. JSP containers default behavior almost always suffices.

Read More →