JSP Page Directive

This directive is used to provide instructions to the container related to the current page. This directive can be coded anywhere in the JSP but formal users usually used to do it in the top of the page.


Syntax of page directive can be written as below:

<%@ page attribute="value" %>

Or in the form of XML equivalent:

<jsp:directive.pageattribute="value"/>

 

The following are the list of attributes associated with the page directive:

Attribute Purpose
buffer Specifes the size of the buffer
autoFlush Determines when the buffer should be flushed
contentType Used to set the content type of a JSP page
errorPage Gives the URL of another JSP that has an error.
isErrorPage Indicates if this JSP page is a URL specified by another JSP page’s errorPage attribute.
extends Specifies a superclass that the generated servlet must extend
import Specifies a list of packages or classes for use in the JSP as the Java import statement does for Java classes.
info provides a description to a JSP page
isThreadSafe Defines the threading model if muti-threading can be used.
language Defines the programming language used in the JSP page.
session Specifies whether or not the JSP page participates in HTTP sessions
isELIgnored This attribute specify whether expressions will be evaluated or not
isScriptingEnabled Determines if scripting elements are allowed for use.
pageEncoding  specifies the language that the page uses when the page is sent to the browser

 

buffer Attribute:

This attribute is used to specify the size of buffer. When we give none to buffer then the output will be written to response object and to the JSPWriter. For example if we specify some value for buffer then the output is written to buffer and then to the response object. You may code a buffer value of “none” to specify no buffering so that all servlet output is immediately write into the response object .

<%@ page buffer="none" %>

 

Use the following to direct the servlet to write output to a buffer of size not less than 8 kilobytes:

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

 

autoFlush Attribute:

The autoflush attribute of page directive is used to indicate JSP containers behaviour when the buffer gets full. The autoflush attribute takes only boolean values true or false as input. The default value for autoflush is true.  If autoflush is set to true, if the buffer gets full, JSP container will flush the data into response object.

<%@ page autoFlush="true" %>

The following directive causes the servlet to throw an exception when the servlet’s output buffer is full:

<%@ page autoFlush="false" %>

Usually, the buffer and autoFlush attributes are coded on a single page directive as follows:

<%@ page buffer="16kb" autoflush="true" %>

 

The contentType Attribute:

We can use this attribute to specify content type and character set of the response.  The default value of the content type attribute is  “text/html” and Charest is ISO-8859-1.

Usually the most common syntax of the contentType attribute being used is as below:

<%@ page contentType="text/html:charset=ISO-8859-1" %>

Even in the Eclipse EE for Java developers we will find these as the default attributes being set.

There might be other possible types of contentType.

  1. Application/msword -> msword
  2. Test/HTML -> HTML file
  3. Test/xml -> XML file
  4. Text/plain -> text file

 

errorPage Attribute:

If we want to place errors in a different page then the URL to the error page can be mentioned in this attribute as errorPage. In simple it is used to display the page if there is an error while the current page runs. The value of the errorPage attribute is a relative URL.

<%@ page errorPage="errorpage.jsp" %>

iserrorPage Attribute:

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 the isErrorPage attribute is false.

For example, the handleError.jsp sets the isErrorPage option to true because it is supposed to handle errors:

<%@ page isErrorPage="true" %>

 

extends Attribute:

The extends attribute specifies a super class that the generated servlet must extend.

The following directive directs the JSP translator to generate the servlet such that the servlet extends Package1.Class1:

<%@ page extends="Package1.Class1" %>

 

import Attribute:

We can use import attribute for importing classes & interfaces present in a package.  This is similar to Core java import statement.

To import java.sql.*, use the following page directive:

<%@ page import="java.sql.*" %>

To import multiple packages you can specify them separated by comma as follows:

<%@ page import="java.sql.*,java.util.*"  %>

 

To import multiple packages the following are various possibilities.

<%@ page import = "java.util.*" %>
<%@ page import = "java.io.*" %>
<%@ page import = "java.util.*" import = "java.io.*" %>
<%@ page import = "java.util.*, java.io.*"  %>

Within the same Jsp we are not allowed to take any attribute except import multiple times with different values.  But we can take multiple times with same value.

<%@pagesession="true" session = 'true'%>

// one  attribute with same value à This is allowed.

 

<%@pagesession="true" session = 'false'%>

// same attribute with different value à This is not allowed.

In Invalid   case we will get Translation time error saying page directive: illegal to have multiple occurrences of session with different values.

Inside Jsp it is not required to import the following packages because these are available by default in every Jsp.

 

1.     javax. servlet

2.     javax. servlet.http

3.     javax. servlet.jsp

4.     java.lang.*

 

info attribute:

1.     This attribute provide the information regarding current jsp page.

2.     The JSP container ignores the info attribute.

3.     The info attribute is meant for JSP programmers to help them understand about the functionality of the page.

 

<%@ page info= "This article is for Page Directives" %>

 

isThreadSafe attribute:

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.

 

<%@ page isThreadSafe="true" %>

If the value of this attribute is set to true. It implies that the JSP container can handle or send multiple concurrent client requests to the JSP page by starting a new thread.

 

<%@ page isThreadSafe="false" %>

 

If the value of this attribute is set to false, then the JSP container sends client requests only one at a time to the JSP page.

 

language attribute:

1.     JSP containers may support languages other than Java. However most popular JSP container support only Java.

2.     The language attribute of page directive is used to specify the scripting language.

 

<%@ 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.

session attribute:

  1. The session attribute indicates whether or not the JSP page uses HTTP sessions.
  2. In every Jsp session implicit object is by default available.
  3. A value of true means that the JSP page has access to a built-in session object
  4. A value of false means that the JSP page cannot access the built-in session object.
  5. If we don’t want, we can make it unavailable by declaring page directives as follows.
  6. The default value of the session attribute is true.

Generally while building a user interactive JSP application, we make sure to give access to the user to get hold of his/her personal data till the session is active. Consider an example of logging in into your bank account, we can access all of your data till we signout (or session expires). In order to maintain session for a page the session attribute should be true.

 

<%@ page session="true" %>

 

isELIgnored Attribute:

 

  1. In JSP 1.2 version expression languages was introduced. The main objective of Expression Language is to eliminate java, Code from the JSP.
  2. The isELIgnored attribute takes boolean value of true or false as input.
  3. If isELIgnored is true, then any Expression Language in JSP is ignored.
  4. The default value of isELIgnored is false.
  5. In JSP 1.2 version the default value of  isELIgnored is  true, but JSP 2.0    version on wards the default value us false.
<%@ page isELIgnored="false" %>

 

According to JSP 1.2 version i.e. that we are using now:

If we make use of the below code in our JSP program with isELIgnored as  true

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1" isELIgnored="true"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Additon : ${2+3}

</body>
</html>

 

The output of the above code will be:

isELIgnored

And if we change the value to “false” then

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1" isELIgnored="false"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
Additon : ${2+3}
 
</body>
</html>

The output of the above code will be:

isELIgnored

isScriptingEnabled attribute:

The isScriptingEnabled attribute determines if scripting elements are allowed for use.

The default value (true) enables scriptlets, expressions, and declarations. If the attribute’s value is set to false, a translation-time error will be raised if the JSP uses any scriptlets, expressions (non-EL), or declarations.

You can set this value to false if you want to restrict usage of scriptlets, expressions (non-EL), or declarations:

<%@ page isScriptingEnabled="false" %>

pageEncoding attribute:

This attribute specifies the language that the page uses when the page is sent to the browser. This attribute works like the meta tag of the HTML markup language.

 

<%@ page pageEncoding="ISO-8859-1" %>

 

Posted on July 12, 2014 in Java Server Pages

Share the Story

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top