BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

JSP Directives – Page, Include and TagLib

Last Updated: July 27, 2022 by Chaitanya Singh | Filed Under: JSP tutorial

JSP Directives control the processing of an entire JSP page. It gives directions to the server regarding processing of a page. There are three types of directives available in JSP: page, include and taglib. In this guide, you will learn all the three JSP directives in detail with the help of examples.

Syntax of Directives:

 <%@ directive name [attribute name=“value” attribute name=“value” ........]%>

There are three types of Directives in JSP:
1) Page Directive
2) Include Directive
3) TagLib Directive

1) Page Directive

JSP page directive defines the attributes that apply to the whole JSP page. There are several attributes, which are used in Page Directives. These attributes are as follows:

  1. import
  2. session
  3. isErrorPage
  4. errorPage
  5. ContentType
  6. isThreadSafe
  7. extends
  8. info
  9. language
  10. autoflush
  11. buffer

1. import:

This attribute is used to import packages. While doing coding you may need to include more than one packages, In such scenarios this page directive’s attribute is very useful as it allows you to mention more than one packages at the same place separated by commas (,). Alternatively you can have multiple instances of page element each one with different package.

Syntax of import attribute –

<%@page import="value"%>

Here value is package name.

Example of import: The following is an example of how to import more than one package using import attribute of page directive.

 <%@page import="java.io.*%>
 <%@page import="java.lang.*%>
 <%--Comment: OR Below Statement: Both are Same--%>
 <%@page import="java.io.*, java.lang.*"%>

Example of import attribute

<html>
<body>

<%@ page import="java.util.Date" %>
Current Date: <%= new Date() %>

</body>
</html>

2. session:

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 sign out (or session expires). In order to maintain session for a page the session attribute should be true.

This attribute is to handle HTTP sessions for JSP pages. It can have two values: true or false. Default value for session attribute is true, which means if you do not mention this attribute, server may assume that HTTP session is required for this page.

Default value for this attribute: true

Syntax of session attribute:

<%@ page session="value"%>

here value is either true OR false

Examples of session:

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

3. isErrorPage:

This attribute is used to specify whether the current JSP page can be used as an error page for another JSP page. If value of isErrorPage is true it means that the page can be used for exception handling for another page. Generally these pages has 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 has isErrorPage set to true. If the value is false, the page cannot use exception implicit object.

Default value: 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, a exception handling page.

4. errorPage:

As I stated above, when 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.  errorPage attribute is used to specify the URL of a JSP page which has isErrorPage attrbute set to true. It  handles the un-handled exceptions in the page.

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="ExceptionHandling.jsp"%>

This means if any exception occurs on the JSP page where this code has been placed, the ExceptionHandling.jsp (this page should have isErrorPage true) page needs to be called.

5. contentType:

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"%>

Example of contentType attribute

<html>
<body>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html" %>
Current Date: <%= new java.util.Date() %>

</body>
</html>

6. isThreadSafe:

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 only single thread will execute the page code.

Default value for isThreadSafe attribute: true.

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.

7. buffer:

This attribute is used to specify the buffer size. If you specify this to none during coding then the output would directly written to Response object by JSPWriter. And, if you specify a buffer size then the output first written to buffer then it will be available for response object.

Syntax of buffer attribute:  

<%@ page buffer="value"%>

value is size in kb or none.

Example of buffer:

No buffer for this page:

<%@ page buffer="none"%>

5 kb buffer size for the page, which has below code:

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

Example of buffer attribute

<html>
<body>

<%@ page buffer="30kb" %>
Current Date: <%= new java.util.Date() %>

</body>
</html>

8. extends:

Like java, here also this attribute is used to extend(inherit) the class.

Syntax of extends attribute: 

<%@ page extends="value"%>

Value is package_name.class_name.

Example of extends:

The following code will inherit the SampleClass from package: mypackage

<%@ page extends="mypackage.SampleClass"%>

9. info:

It provides a description to a JSP page. The string specified in info will return when we will call  getServletInfo() method.

Syntax of info:

<%@ page info="value"%>

here value is Message or Description

Example of info attribute:

<%@ page info="This code is given by Chaitanya Singh"%>

Example of info attribute

<html>
<body>

<%@ page info="This code is given by Chaitanya Singh" %>
Current Date: <%= new java.util.Date() %>

</body>
</html>

10. language:

It specifies the scripting language( underlying language) being used in the page.

Syntax of language:

<%@ page language="value"%>

value is scripting language here.

Example of language attribute:

<%@ page language="java"%>

11. autoFlush:

If it is true it means the buffer should be flushed whenever it is full. false will throw an exception when buffer overflows.

Default value: True

Syntax of autoFlush:

<%@ page autoFlush="value"%>

value can be true or false.

Example of autoFlush attribute:

Buffer will be flushed out when it is full –

<%@ page autoFlush="true"%>

It will throw an exception when buffer is full due to overflow condition

<%@ page autoFlush="true"%>

12. isScriptingEnabled:

It has been dropped and not in use.

13. isELIgnored:

This attribute specify whether expressions will be evaluated or not.

Default value: true

Syntax of isELIgnored:

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

Expression will be evaluated (true is a default value so no need to specify)-

<%@ page isELIgnored="true"%>

2) Include Directive

Include directive is used to copy the content of one JSP page to another. It’s like including the code of one file into another.

Syntax of Include Directive:

<%@include file ="value"%>

here value is the JSP file name which needs to be included. If the file is in the same directory then just specify the file name otherwise complete URL(or path) needs to be mentioned in the value field.

Note: It can be used anywhere in the page.

Example:

<%@include file="myJSP.jsp"%>

You can use the above code in your JSP page to copy the content of myJSP.jsp file. However in this case both the JSP files must be in the same directory. If the myJSP.jsp is in the different directory then instead of just file name you would need to specify the complete path in above code.

3) Taglib Directive

This directive basically allows user to use Custom tags in JSP. we shall discuss about Custom tags in detail in coming JSP tutorials.  Taglib directive helps you to declare custom tags in JSP page.

Syntax of Taglib Directive:

<%@taglib uri ="taglibURI" prefix="tag prefix"%>

Where URI is uniform resource locator, which is used to identify the location of custom tag and tag prefix is a string which can identify the custom tag in the location identified by uri.

Example of Targlib:

<%@ taglib uri="http://www.sample.com/mycustomlib" prefix="demotag" %>
<html>
<body>
<demotag:welcome/>
</body>
</html>

As you can see that uri is having the location of custom tag library and prefix is identifying the prefix of custom tag.
Note: In above example: <demotag: welcome> has a prefix demotag.

❮ JSP exceptionJSP include directive ❯

Top Related Articles:

  1. JSP Scriptlet Tag (Scripting elements)
  2. JSP Implicit Objects
  3. Response Implicit Object in JSP with examples
  4. Out Implicit Object in JSP with examples
  5. Include Directive in JSP

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Vin says

    July 23, 2014 at 6:51 AM

    Nice Tutorial.

    Reply
  2. Poornima says

    November 5, 2014 at 5:10 PM

    It will throw an exception when buffer is full due to overflow condition

    Sir I think in this case autoFlush=”false” may be there was a typing mistake.

    Reply
  3. Thivya says

    November 21, 2015 at 6:36 AM

    Hi, first of all I would like to thank you for this great tutorial, Its really helpful for me to prepare for the interviews.
    I have a query on the topic “Diff b/w include directive and include action tag”. I am writing here as I am unable to find the comments box over there. You mentioned that for include directive, the jsp wil included during translation phase whereas for include action tag it will be at runtime. My query is for include action tag, when the included JSP will get translated?

    Reply

Leave a Reply Cancel reply

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

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap