Categories: JSPTutorial

JSP Page Directive

Page Directives in JSPs are instruction from JSPs to JSP Engine. The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code page directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page.

General syntex of JSP Page Directives generally look like this:

<%@ page attribute1="value1" attribute2="value2" %>

Syntex:

<%@ page import="xyz" language="xyz" extends="xyz" session="xyz" buffer="xyz" isThreadSafe="xyz" autoFlush="xyz" errorPage="xyz" info="xyz" contentType="xyz" isErrorPage="xyz" %>

Example:

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

Explanation: As shown above, Page directives has many attributes. Page Directive can be used multiple times in a single JSP. However, except import attribute, no other attirbute can be used multiple times.

language Attribute: At present JSP supports only JAVA as a language attirbute.

<%@ page language="java" %>

import Attribute: import Attribute in Page Directive allows to import JAVA packages to JSP. You can import more than one package as comma seperated values or you can write multiple Page Directive each having its own import attribute.

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

multiple import

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

extends Attribute: extends Attribute allows to extend Super Class in JSP.

<%@ page extends="com.dineshonjava.jsp.SomeClass"  %>

session Attribute: session Attribute can have either true or false value. Default value is always true. If it is set to false, session object can not be used in JSP.
he session attribute indicates whether or not the JSP page uses HTTP sessions. A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object.

Following directive allows the JSP page to use any of the builtin object session methods such as session.getCreationTime() or session.getLastAccessTime():

<%@ page session="true" %>

buffer Attribute: You can specify either none or some value in kb. Default value is 8kb. This value is used by implicit object “out” to buffer output from compiled JSP to client’s web browser.

<%@ page buffer="none" %>

autoFlush Attribute: autoFlush Attribute in JSP has either true or false value. Default value is set to True. That means when buffer overflows, it automatically flush the buffer. If it is set to false, JSP will throw an error when buffer overflows.

<%@ page autoFlush="false" %>

This directive causes the servlet to flush the output buffer when full:

<%@ page autoFlush="true"%>

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

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

isErrorPage Attribute: isErrorPage Attribute in Page Directive has either true or false value. Default value is set to false. Implicit object exception can not be used if isErrorPage value is false. In order to use exception object in JSP, isErrorPage attribute value must be set to true.

<%@ page isErrorPage="true"%>

errorPage Attribute: errorPage Attribute in Page Directive points to the page which handles exception thrown in current jsp. Location of the errorPage jsp is shown using relative path.

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

contentType Attribute: contentType Attribute in Page Directive suggest the MIME Type and Character Encoding for JSP. Default value for MIME Type is text/html and default value for Character Encoding is ISO-8859-1.

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

The following directive sets the content type as a Microsoft Word document:

<%@ page contentType="application/msword"%>

isThreadSafe Attribute: isThreadSafe Attribute in Page Directive has either true or false value. Default value is set to true. That means, JSP engine sends mutiple requests to JSP same time. Code inside JSP must be synchronized to handle concurrent reqests. If the value of the attribute is set to false, JSP Engine will send only one reqest at a time to JSP.

<%@ page isThreadSafe="false"%>

info Attribute: info Attribute in Page Directive is just a text String.

<%@ page info="This is JSP Page"%>

<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago