JSP Directives: Page, Include & Taglib Tutorial

What are JSP Directives?

Syntax of Directive:

<%@ directive attribute="" %>

There are three types of directives:

  1. Page directive
  2. Include directive
  3. Taglib directive

Each one of them is described in detail below with examples:

In this tutorial, you will learn -

JSP Page directive

Syntax of Page directive:

<%@ page…%>

Following are its list of attributes associated with page directive:

  1. Language
  2. Extends
  3. Import
  4. contentType
  5. info
  6. session
  7. isThreadSafe
  8. autoflush
  9. buffer
  10. IsErrorPage
  11. pageEncoding
  12. errorPage
  13. isELIgonored

More details about each attribute

  1. language: It defines the programming language (underlying language) being used in the page.

    Syntax of language:

    <%@ page language="value" %>

    Here value is the programming language (underlying language)

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

Explanation of code: In the above example, attribute language value is Java which is the underlying language in this case. Hence, the code in expression tags would be compiled using java compiler.

  1. Extends: This attribute is used to extend (inherit) the class like JAVA does

Syntax of extends:

<%@ page extends="value" %>

Here the value represents class from which it has to be inherited.

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	   pageEncoding="ISO-8859-1"%>

<%@ page extends="demotest.DemoClass" %>

Explanation of the code: In the above code JSP is extending DemoClass which is within demotest package, and it will extend all class features.

  1. Import: This attribute is most used attribute in page directive attributes.It is used to tell the container to import other java classes, interfaces, enums, etc. while generating servlet code.It is similar to import statements in java classes, interfaces.

Syntax of import:

<%@ page import="value" %>

Here value indicates the classes which have to be imported.

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    import="java.util.Date" pageEncoding="ISO-8859-1"%>

Explanation of the code:

In the above code, we are importing Date class from java.util package (all utility classes), and it can use all methods of the following class.

  1. contentType:

Syntax of the contentType:

<%@ page contentType="value" %>

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

Explanation of the code:

In the above code, the content type is set as text/html, it sets character encoding for JSP and for generated response page.

  1. info

Syntax of info:

<%@ page info="value" %>

Here, the value represents the servlet information.

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    info="Guru Directive JSP" pageEncoding="ISO-8859-1"%>

Explanation of the code:

In the above code, string "Guru Directive JSP" can be retrieved by the servlet interface using getServletInfo()

  1. Session

Syntax of session:

<%@ page session="true/false"%>

Here in this case session attribute can be set to true or false

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    session="false"%>

Explanation of code:

In the above example, session attribute is set to "false" hence we are indicating that we don't want to create any session in this JSP

  1. isThreadSafe:

Syntax of isThreadSafe:

<% @ page isThreadSafe="true/false" %>

Here true or false represents if synchronization is there then set as true and set it as false.

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    isThreadSafe="true"%>

Explanation of the code:

In the above code, isThreadSafe is set to "true" hence synchronization will be done, and multiple threads can be used.

  1. AutoFlush:

This attribute specifies that the buffered output should be flushed automatically or not and default value of that attribute is true.

If the value is set to false the buffer will not be flushed automatically and if its full, we will get an exception.

When the buffer is none then the false is illegitimate, and there is no buffering, so it will be flushed automatically.

Syntax of autoFlush:

<% @ page autoFlush="true/false" %>

Here true/false represents whether buffering has to be done or not

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    autoFlush="false"%>

Explanation of the code:

In the above code, the autoflush is set to false and hence buffering won't be done and it has manually flush the output.

  1. Buffer:

Syntax of buffer:

<%@ page buffer="value" %>

Here the value represents the size of the buffer which has to be defined. If there is no buffer, then we can write as none, and if we don't mention any value then the default is 8KB

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    buffer="16KB"%>

Explanation of the code:

In the above code, buffer size is mentioned as 16KB wherein the buffer would be of that size

  1. isErrorPage:

Syntax of isErrorPage:

<%@ page isErrorPage="true/false"%>

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    isErrorPage="true"%>

Explanation of the code:

In the above code, isErrorPage is set as true. Hence, it will check any other JSPs has errorPage (described in the next attribute) attribute set and it can handle exceptions.

  1. PageEncoding:
The "pageEncoding" attribute defines the character encoding for JSP page.

The default is specified as "ISO-8859-1" if any other is not specified.

Syntax of pageEncoding:

<%@ page pageEncoding="vaue" %>

Here value specifies the charset value for JSP

Example:

<%@ page language="java" contentType="text/html;" pageEncoding="ISO-8859-1"
    isErrorPage="true"%>

Explanation of the code:

In the above code "pageEncoding" has been set to default charset ISO-8859-1

  1. errorPage:
This attribute is used to set the error page for the JSP page if JSP throws an exception and then it redirects to the exception page.

Syntax of errorPage:

<%@ page errorPage="value" %>

Here value represents the error JSP page value

Example:

<%@ page language="java" contentType="text/html;" pageEncoding="ISO-8859-1"
    errorPage="errorHandler.jsp"%>

Explanation of the code:

In the above code, to handle exceptions we have errroHandler.jsp

  1. isELIgnored:

Syntax of isELIgnored:

<%@ page isELIgnored="true/false" %>

Here, true/false represents the value of EL whether it should be ignored or not.

Example:

<%@ page language="java" contentType="text/html;" pageEncoding="ISO-8859-1"
    isELIgnored="true"%>

Explanation of the code:

In the above code, isELIgnored is true and hence Expression Language (EL) is ignored here.

In the below example we are using four attributes(code line 1-2)

Example with four attributes

<%@ page language="java" contentType="text/html;" pageEncoding="ISO-8859-1"
    isELIgnored="false"%>
 <%@page import="java.util.Date" %>
    
<!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>Directive Guru JSP1</title>
</head>
<body>
<a>Date is:</a>
<%= new java.util.Date() %>
</body>
</html>

Explanation of the code:

Code Line 1-2: Here we have defined four attributes i.e.

Code Line 3: Here we have used import attribute, and it is importing "Date class" which is from Java util package, and we are trying to display current date in the code.

When you execute the above code, you will get the following output

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

JSP Include directive

Syntax of include directive:

<%@ include….%>

Example:

Directive_jsp2.jsp (Main file)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ include file="directive_header_jsp3.jsp" %>
<!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>Guru Directive JSP2</title>
</head>
<body>
<a>This is the main file</a> 
</body>
</html>

Directive_header_jsp3.jsp (which is included in the main file)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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">

</head>
<body>
<a>Header file : </a>
<%int count =1; count++;
out.println(count);%> :
</body>
</html>

Explanation of the code:

Directive_jsp2.jsp:

Code Line 3: In this code, we use include tags where we are including the file directive_header_jsp3.jsp into the main file(_jsp2.jsp)and gets the output of both main file and included file.

Directive_header_jsp3.jsp:

Code Line 11-12: We have taken a variable count initialized to 1 and then incremented it. This will give the output in the main file as shown below.

When you execute the above code you get the following output:

JSP Actions: JSP Implicit objects,Page directive, Include directive, Taglib Directive

Output:

JSP Taglib Directive

Syntax of taglib directive:

<%@ taglib uri="uri" prefix="value"%>

Here "uri" attribute is a unique identifier in tag library descriptor and "prefix" attribute is a tag name.

Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="gurutag" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Guru Directive JSP</title>
<gurutag:hello/>
</head>
<body>
</body>
</html>

Explanation of the code:

Code Line 3: Here "taglib" is defined with attributes uri and prefix.

Code Line 9: "gurutag" is the custom tag defined and it can be used anywhere

 

YOU MIGHT LIKE: