JSP page Directive

This article is a series of JSP Tutorial. In this article, we will learn how to use JSP page directive with an example.

The page directive is used to specify attributes for the JSP page as a whole. It has the following syntax:
<%@ page [attribute="value" attribute="value" ...] %>
where the attributes can be any of the following:
  • import
  • contentType
  • extends
  • info
  • buffer
  • language
  • isELIgnored
  • isThreadSafe
  • autoFlush
  • session
  • pageEncoding
  • errorPage
  • isErrorPage
Let's discuss each attribute of page directive with an example.

import

The import attribute is used to describe the fully qualified names of classes used in the JSP page. This makes it possible for the classes to be referred to by their classes names without the package prefix. This is an optional attribute.
A comma-separated list of one or more package.* names and/or fully qualified class names. This list is used to create corresponding import statements in the generated Java servlet. The following packages are automatically included and need not be specified:
java.lang.*
java.servlet.*
java.servlet.jsp.*
java.servlet.http.*
Example: Consider we have Calculator.java class under net.javaguides.jsp.tutorial package. Below example shows how to use import attribute:
public class Calculator {
 public int addition(int num1, int num2) {
  return (num1 + num2);
 }
}
Here is JSP file with the usage of import attribute:
<%@ page import="net.javaguides.jsp.tutorial.Calculator"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <% Calculator calculator = new Calculator(); %>
    Addition of 20 + 10 = <%= calculator.addition(20, 10) %>

</body>
</html>

session

The session attribute of the page directive indicates whether the page requires an HTTP session. Two values are possible:
  • session="true" if the page needs an HTTP session. This is the default value.
  • session="false" if no HTTP session is required. If this is specified, the session implicit variable is undefined and will cause a translation error if used.
Examples of a session attribute:
<%@ page session="true"%>
The above code would allow a page to have session implicit objects.
<%@ page session="false"%>
If this code is specified in a JSP page, it means session objects will not be available for that page. Hence session cannot be maintained for that page.

isErrorPage

This attribute is used to specify whether the current JSP page can be used as an error page for another JSP page. If a value of isErrorPage is true it means that the page can be used for exception handling for another page. Generally, these pages have error/warning messages OR exception handling codes and being called by another JSP page when there is an exception occurred there.
There is another use of isErrorPage attribute – The exception implicit object can only be available to those pages which have isErrorPage set to true. If the value is false, the page cannot use exception implicit object. The default value of this attribute is false.
Syntax of isErrorPage attribute:
<%@ page isErrorPage="value"%>
Here value is either true OR false.
Example of isErrorPage:
<%@ page isErrorPage="true"%>
This makes a JSP page, an exception handling page.

errorpage

When an isErrorPage attribute is true for a particular page then it means that the page can be called by another page in case of an exception. The errorPage attribute is used to specify the URL of a JSP page which has an isErrorPage attribute set to true. It handles the unhandled exceptions in the page.
The syntax of errorPage attribute:
<%@ page errorPage="value"%>
Here value is a JSP page name which has exception handling code (and isErrorPage set to true).
Example of errorPage:
<%@ page errorPage="404.jsp"%>
<%@ page errorPage="500.jsp"%>
This means if any exception occurs on the JSP page where this code has been placed, the 404.jsp (this page should have isErrorPage true) page needs to be called.

contentType

A JSP page ordinarily generates HTML output, but other content types can also be produced. This attribute is used to set the content type of a JSP page.
Default value: text/html
Syntax of contentType attribute:
<%@ page contentType="value"%>
here value of content type can be anything such as: text/html, text/xml etc.
Example of contentType:
Below code can be used for text/html pages.
<%@ page contentType="text/html"%>
for text/xml based pages:
<%@ page contentType="text/xml"%>

isThreadSafe

By default, servlet engines load a single instance of a servlet and use a pool of threads to service individual requests. This means two or more threads can be executing the same servlet methods simultaneously. If the servlet has instance variables, and if no provision is made to synchronize access, the threads can collide and interfere with each others’ access to the variables.
Let's understand this with an example. Suppose you have created a JSP page and mentioned isThreadSafe as true, it means that the JSP page supports multithreading (more than one thread can execute the JSP page simultaneously). On the other hand, if it is set to false then JSP engine won’t allow multithreading which means the only single thread will execute the page code.
A default value for isThreadSafe attribute: true.
The syntax of isThreadSafe attribute:
<%@ page isThreadSafe="value"%>
here value can be true OR false.
Example of isThreadSafe:
<%@ page isThreadSafe="false"%>
Only one thread will be responsible for JSP page execution.

buffer

Specifies the size of the output buffer. Valid entries are nnnkb or none, where nnn is the number of kilobytes allocated for the buffer. The default value is 8kb.
Example of buffer:
No buffer for this page:
<%@ page buffer="none"%>
5 kb buffer size for the page, which has below code:
<%@ page buffer="15kb"%>

extends

The fully qualified name of the superclass of this JSP page. This must be a class that implements the HttpJspPage interface. The JSP specification warns against the use of this attribute without fully understanding its implications.
Example of extends:
The below code will inherit the SampleClass from a package: net.javaguides.tutorial
<%@ page extends="net.javaguides.tutorial.ParentClass"%>

info

The info attribute of the page directive lets you specify descriptive information about the JSP page. The string specified in info will return when we will call getServletInfo() method.
The syntax of info attribute:
<%@ page info="value"%>
here value is Message or Description
Example of info attribute:
<%@ page info="This is JSP tutorial by JavaGuides"%>

language

The language attribute specifies the scripting language used in the JSP page. The default value is "java".
<%@ page language="java"%>

autoFlush

If it is true it means the buffer should be flushed whenever it is full. false will throw an exception when buffer overflows.
The default value is true. 
Example:
<%@ page autoFlush="true"%>

isELIgnored

This attribute specifies whether expressions will be evaluated or not. The default value is true
The syntax of isELIgnored attribute:
<%@ page isELIgnored="value"%>
value can be true or false.
Example of isELIgnored attribute:
Any expression present inside JSP page will not be evaluated –
<%@ page isELIgnored="false"%>
The expression will be evaluated (true is a default value so no need to specify)-
<%@ page isELIgnored="true"%>
In that next article, we will learn how to use JSP include directive with an example.

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments