Updates
  • Starting New Weekday Batch for Full Stack Java Development on 27 September 2025 @ 03:00 PM to 06:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM

JSP Directives

A directive is a special instruction to the container to perform the required task.

There are three directives in JSP.

1. include a directive
2. page directive
3. taglib directive

1. include directive

The include directive is used to include one JSP or HTML in another JSP. The include directive line will be replaced with the content of the included JSP or HTML at translation time.
You can write one or more include directives inside the JSP.

Syntax:

<%@ include file="some jsp or html "%>

EX:

<%@ include file="header.html"%>
<%@ include file="login.jsp"%>
<%@ include file="footer.html"%>

Files Required For Program

1. header.jsp
2. footer.html
3. home.jsp
4. web.xml

1. header.jsp

            
<header>
    <h3> JTCBookstore</h3>
    <h3> JTCBookstore</h3>
    <br/><br/>
</header>
        
            

2. footer.html

            
<footer>
    <h3>JTC All Rights Reserved.2023</h3>
    <h3>JTC All Rights Reserved.2023</h3>
</footer>
        
            

3. home.jsp

            
<!DOCTYPE html>
<html>
   <body>
      <%@ include file="header.jsp" %> 
      <main>
         <h3> Display the Books Here </h3>
         <h3> Display the Books Here </h3>
         <h3> Display the Books Here </h3>
         <h3> Display the Books Here </h3>
         <br />
         <br />
      </main>
      <%@ include file="footer.html" %>
   </body>
</html>
        
            

4. web.xml

            
<?xml version="1.0" encoding="UTF-8"?>
<web-app …>
<display-name>Lab3</display-name>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
</web-app>
        
            
page directive

Syntax:

<%@ page language="" import="" session="" extends="" isThreadSafe="" errorPage="" isErrorPage="" isELIgnored="" %>

language:

language attribute is used to specify the language for the scriptlets and declarations.
Currently valid value is java and is the default value.
<%@ page language="java" %> VALID
<%@ page language="c" %> INVALID (invalid language attribute )

import:

import attribute is used to sepcify the packages to be imported for the translated servlet.
You can specify one or more packages with comma(,) separation using this attribute.
<%@ page import="java.util.*,java.io.*" %>

session:

session attribute is used to enable or disable session object in the JSP.
By default session object is enabled in JSP.
If you want to disable use the following:
<%@ page session="false" %>

Files Required For Program

1. hello.jsp
2. hai.jsp

1. hello.jsp

            
<%@ page language="java" session="true" import="java.util.*,java.io.*,java.sql.*"%> 
<!DOCTYPE html> 
<html>
   <body>
      <h2> JTC BookStore</h2>
      <h3> Welcome to JTC </h3>
      <% 
         int a=10; 
         int b=20; 
         int sum=a+b; 
         out.println("Sum : "+sum); 
         %> 
      <h3> Welcome to JTC </h3>
      <h3> Session ID: <%= session.getId() %></h3>
   </body>
</html>
        
            

2. hai.jsp

            
<%@ page session="false" %> 
<%@ page import="java.util.*"%> 
<!DOCTYPE html> 
<html>
   <body>
      <h2> JTC BookStore</h2>
      <h3> Welcome to JTC </h3>
      <% 
         int a=10; 
         int b=20; 
         int sum=a+b; 
         out.println("Sum : "+sum); 
         %> 
      <h3> Welcome to JTC </h3>
   </body>
</html>
        
            

extends:

HttpJspBase is the default super class for the translated servlet.
If you want to use other super class for the translated servlet instead of HttpJspBase , use extends attribute.
When you take HttpServlet as a super class then you have to override any of the methods of HttpServlet.
<%@ page extends="javax.servlet.http.HttpServlet" %>

Files Required For Program

1. test1.jsp
2. test2.jsp
3. test3.jsp

1. test1.jsp

            
<%@page import="java.io.*"%> 
<%@ page extends="javax.servlet.http.HttpServlet"%>
<!DOCTYPE html> 
<html>
   <body>
      <h2> JTC BookStore</h2>
      <h3> Welcome to JTC </h3>
   </body>
</html>
        
            

2. test2.jsp

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

2. test3.jsp

            
<%@page import="java.io.*"%> 
<%@ page extends="javax.servlet.http.HttpServlet"%> 
<!DOCTYPE html> 
<html>
   <body>
      <h2> JTC BookStore</h2>
      <%! 
         public void service(HttpServletRequest request, 
         HttpServletResponse response) throws 
         IOException, ServletException{ 
         PrintWriter out= response.getWriter(); 
         response.setContentType("text/html"); 
         out.println("<h2> I am service()</h2> "); 
          } 
         %> 
      <h3> Welcome to JTC </h3>
   </body>
</html> 
        
            

isThreadSafe :

This attribute is used to sepcify the Servlet Thread model required.
Default Servlet Thread model is multi-thread model.
Default value of isThreadSafe attribute is true.
If you want to follow single thread model , use this attribute value as false.
<%@ page isThreadSafe="false" %>
If you want to follow multi thread model , use this attribute value as true.
<%@ page isThreadSafe="true" %>

Files Required For Program

1. test1.jsp
2. test2.jsp

1. test1.jsp

            
<%@ page isThreadSafe="false"%> 
<!DOCTYPE html> 
<html>
   <body>
      <h2> JTC BookStore</h2>
      <h3> Welcome to JTC </h3>
   </body>
</html>
        
            

1. test2.jsp

            
<%@ page isThreadSafe="true"%> 
<!DOCTYPE html> 
<html>
   <body>
      <h2> JTC BookStore</h2>
      <h3> Welcome to JTC </h3>
   </body>
</html>
        
            

errorPage and isErrorPage :

If want to handle the excepions in a centralized JSP then use these two attributes.
errorPage is used to specify the centralized JSP.
isErrorPage is used to enable the exception implicit object in the centralized JSP used for error handling.

Files Required For Program

1. test1.jsp
2. test2.jsp
3. myerrors.jsp

1. test1.jsp

            
<%@ page errorPage="myerrors.jsp"%> 
<!DOCTYPE html> 
<html>
   <body>
      <h2> I am test1.jsp</h2>
      <% 
         int a=10; 
         int b=0; 
         int x= a/b; 
         out.println("x = "+x); 
         %> 
      <h3> Welcome to JTC </h3>
   </body>
</html>
        
            

2. test2.jsp

            
<%@ page errorPage="myerrors.jsp"%> 
<!DOCTYPE html> 
<html>
   <body>
      <h2> I am test2.jsp</h2>
      <% 
         int arr[] = {10,20,30}; 
         out.println("Last Element = "+arr[arr.length]); 
         %> 
      <h3> Welcome to JTC </h3>
   </body>
</html>
        
            

3. myerrors.jsp

            
<%@ page isErrorPage="true"%> 
<%@ page isELIgnored="false"%> 
<!DOCTYPE html> 
<html>
   <body>
      <h2> I am myerrors.jsp</h2>
      <h1> 1. Java Code</h1>
      <% 
         out.println("<h3>"+exception+"</h3>"); 
         out.println("<h3>"+exception.getMessage()+"</h3>"); 
         %> 
      <hr/>
      <h1> 2. JSP Expressions</h1>
      <h3> <%= exception %> </h3>
      <h3> <%= exception.getMessage() %> </h3>
      <hr/>
      <h1> 3. EL Expressions</h1>
      <h3> ${exception} </h3>
      <h3> ${exception.message} </h3>
   </body>
</html>
        
            

isELIgnored :

isELIgnored attribute is used to enable or disable EL expressions.

Note:Upto Tomcat 5.0, EL Expressions are ingnored by default. You have to enable them. From Tomcat 6.0, EL Expressions are enabled by default.

taglib directive

taglib directive allows to use the custom tags in JSP.