page Directive


page directive defines the attributes that are applied to current jsp page at the time of translation jsp page into servlet. It has the following syntax -
<%@ page attribute_1="value_1" attribute_2="value_2" ... %>

page directive can have one or more attributes.        


page directive can have following 11 attributes -

Attribute Name Value Type Default Value
import fully qualified package name (or class name) None
extends class name None
contentType MIME type "text/html;charset=ISO-8859-1"
Info String Depends on JSP Container
language scripting language name "java"
errorPage url None
isErrorPage boolean "false"
session boolean "true"
isThreadSafe boolean "true"
buffer buffer size "8Kb" (8192 characters)
autoFlush boolean "true"
A Page directive can have one or more attributes as follows -
<%@ page import="java.util.*" buffer="6666" %>        
For a JSP Page, We can also specify multiple page attributes in separate lines as follows -
<%@ page import="java.util.*" %>        
<%@ page buffer="6666" %>

We cannot repeat the same attribute multiple time in a single jsp page (Except import attribute).
For example the following statements are invalid -
<%@ page session="false" %>
<%@ page buffer="6666" %>
<%@ page session="true" %>        
However the following statements are valid -
    
<%@ page import="java.util.*" %>
<%@ page buffer="6666" %>
<%@ page import="java.io.*" %>        
We can also import multiple packages in a single statement separated by commas as follows -
    
<%@ page import="java.util.*, java.io.*" %>  

1- import attribute -

import attribute is similar to import statement in plain old java classes. It is used to import class, interface or all members of a package in jsp page at the time of translation of JSP Page into servlet.

For example - to import java.util.Date and java.io.IOException in JSP Page -

<%@ page import="java.util.Date" %>
<%@ page import="java.io.IOException" %>       

At the time of translation of this JSP Page into corresponding servlet, these page attributes will be converted into simple import statements as follows -

import java.util.Date;
import java.io.IOException;        

2- extends attribute -

extends attribute defines a class that the generated servlet inherits. It is rarely used.
It has the following syntax -
<%@ page extends = "package_name.class_Name" %>        
For example - Suppose we have a class named "Demo" in a package named "pkg". To inherit that class in JSP Page -
<%@ page extends = "pkg.Demo" %>        

3- contentType attribute-

contentType is used to set the type of content (Multipurpose Internet Mail Extension (MIME) type) that is coming from server in response to client request. The default value of contentType is "text/html;charset=ISO-8859-1", it is available in _jspService() method of generated servlet.

For Example - To set the content type to "text/html; charset=UTF-8" (i.e. response will contain plain text and html with UTF-8 character encoding)-

<%@page contentType="text/html; charset=UTF-8" %>

At the time of translation of this JSP Page into corresponding servlet, contentType page attributes will be reflected in _jspService() method as follows -

public void _jspService(HttpServletRequest request,
HttpServletResponse response)throws IOException, ServletException {
.
.
.
try {
.
.
.
response.setContentType("text/html; charset=UTF-8");
.
.
.
}
catch (Exception ex) {
.
.
.
}    
}

4- info attribute -

info attribute allows us to define a string of information about current jsp page.

For Example -
<%@page info = "Written by Rohit Kamboj" %>    
This information can later be accessed on the same page by using getServletInfo() method as follows -
<html>
    <body>
        <%@ page info="Written by Rohit Kamboj" %>
        <%
            out.println(getServletInfo());
        %>
    </body>
</html>  
At the time of translation of this JSP Page into corresponding servlet, the value of info attribute will be returned by servlet method getServletInfo() as follows -

public String getServletInfo() {
return "Written by Rohit Kamboj";
}    

5- language attribute -

The language attribute defines the scripting language used in jsp. The default value for language attribute is "java". java is the only option available in Tomcat container. So in case of Tomcat we do not have any other option. However other container may support other languages too, in that case we can select from the available options.

<%@ page language="java" %>
<%
out.println("This is just a demo");
%>  
There will be no effect on the generated servlet because of this attribute.

6- errorPage attribute -

The errorPage attribute defines the error page that will be displayed if an unhandled exception occurred in current JSP page.

For Example - The following JSP Page defines an Error page named "ErroPage.jsp". It will be displayed if some uncaught exception occur -
index.jsp -
<html>
    <body>
        <%@page errorPage="ErrorPage.jsp" %>
        <%
        String str=null;
        int length = str.length();  // It will through a null pointer exception
        %>
    </body>
</html>        
The above code will through a null pointer exception and we have not handled it, so it will display the defined ErrorPage.

7- isErrorPage attribute -

The isErrorPage attribute us used to define a JSP page as Error Page. The default value for isErrorPage is 'false'. TO define a JSP Page as error page, isErroPage should be set to 'true'.
Note - exception implicit object is only available in Error Pages that contain the detail of exception.

For Example - The following JSP Page (ErrorPage.jsp) will be our Error Page used in above example (in errorPage attribute).
<html>
    <body>
        <%@page isErrorPage="true" %>  //define this jsp page as error page
        <h1>You are having an exception in your page!</h1>
        exception is - <%= exception %> //display details of exception by using exception implicit object.
    </body>
</html>    

8- session attribute -

The session attributes defines whether a JSP Page participate in JSP container session management or not. The default value for session attribute is "true" i.e. by default JSP Page participate in session management (session implicit object will be available in jsp page), However we may stop it from participating by setting session to "false" (session implicit object will not be available in jsp page).
It is recommended to stop JSP pages from participating in session management unnecessarily, until session management is not needed because this consumes resources.

For Example - We can set session to "true" in jsp page as follows -
<%@page session="true" %>        
At the time of translation of this JSP Page into corresponding servlet, session will be created in _jspService() method as follows -

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
.
.
.
// declare an HttpSession object reference
HttpSession session = null;
try {
.
.
// create a HttpSession object
session = pageContext.getSession();
.
.
}
catch (Exception ex) {
.
.
}
finally {
.
.
}    
However if session is set to false as follows -
<%@page session="false" %>        
No session object will be created at the time of translation of jsp page into servlet.

9- isThreadSafe attribute -

The isThreadSafe attribute is used to define whether concurrent access to current jsp page is safe or not. The default value for isThreadSafe is "true" i.e. By default concurrent access to jsp page is safe.
For Example -
<%@ page isThreadSafe="true" %>    
If you set it to "false" you are telling the JSP container that you cannot guarantee that concurrent access to this jsp page will be safe.

10 - buffer attribute -

The buffer attribute is used to define the size of buffer used for jsp page contents. It improves the jsp performance. It is defined in Kilobytes and having a default value of "8kb" i.e. 8192 characters.
If you do not want to use buffer, you can set buffer to "none". In that case servlet will directly write the output to response object -
<%@ page buffer = "none" %>        
To set buffer size to 16kb -
 <%@ page buffer = "16kb" %>    
Here we are directing the servlet to write the output to a buffer of size not less than 16 kilobytes.

11- autoFlush attribute -

The autoFlush attribute is related to buffer attribute. It specifies whether the servlet output buffer should be flushed automatically when it is full or raised an exception to indicate that buffer is overflow. The default value for autoFlush is true i.e. buffer will be flushed automatically when it is full.

For Example -
<%@ page autoFlush = "true" %>