Page, Include and Taglib directives in JSP

Page, Include, and Taglib directives in JSP. In this article, we will understand Directives in JavaServer Pages.

JSP Directives

JSP directives are used to set the direction for the web container at the time of translation. JSP directives control the process of completing the JSP page. In short, we can say,

  • Directives are special instructions given to JSP containers on how to translate the JSP page into a servlet.
  • The Directives are represented using <%@ %>

There are three types of directives in JSP:

JSP page directives

The attribute which will define inside JSP page directives will be applied on the complete page. for example, if you want to import some java package.

<%@ page import="value" %>

There are some attributes that are used with page directives.

  • import
  • isErrorPage
  • errorPage
  • isThreadSafe
  • ContentType
  • language
  • autoflush
  • extends
  • buffer
  • info
  • pageEncoding
  • extends
  • session

import

import is used to import a package or interface. it’s the same as java or servlet, for example, we can say if I want to use Date class on my page so I have to import java.util package.

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

Let’s see the example, below code, which will be the print current date. Here, the Date class is imported in JSP page using the <%@ page import=”java.util.*” %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<!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>Insert title here</title>
</head>
<body>
<h1>Welcome to codebun.com</h1>
Today date is <%= new Date() %>
</body>
</html>

isErrorPage

When any unchecked exception occurs in the application, we don’t want the user to see it instead what we can do is we can redirect the user to an error page. So, for this on the JSP page if you want to check if the error page is available or not we can use the isErrorPage attribute.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page isErrorPage="true" %>
    
<!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>Insert title here</title>
</head>
<body>
The exception is: <%= exception %>  
<br>
<%=exception.getClass() %>

</body>
</html>


errorPage

The Error page that we discussed above is specified using the errrorPage attribute. An errorPage attribute is used to redirect control on an error page for example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page errorPage="myErrorPage.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>Insert title here</title>
</head>
<body>
<%= 155/0 %>  
</body>
</html>

isThreadSafe

As we know Java supports the concept of multithreading. Every JSP page is by default thread-safe it means the value of an isThreadSafe attribute is true but forcefully we can set its value false. In this case, If multiple requests come on the page then the container executes them one by one.

The Syntax for isThreadSafe attribute is:

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

ContentType

In a web application, as we know, the client requests something, and the server processes it and gives us back the response. The response here can be an HTML file, image, gif, or any pdf and etc. So, the contentType attribute specifies the MIME type of HTTP response.

For example, Here as we can see the contentType is set to “text/html” which means the HTTP response will be an HTML file.

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

language

This attribute is used to define the type of script language of the page. By default, the language type is Java.

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

buffer

The buffer attribute is used to set the buffer size in kilobytes to handle output which is generated by the JSP page. The default size of the buffer is 8Kb.

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

<!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>Insert title here</title>
</head>
<body>
<%= 155/5 %>  
</body>
</html>

autoflush

autoflush attribute is used to flush the buffered memory automatic. Its boolean type attribute for example:

<%@page buffer="SIZE in kb" autoFlush="true / false" %>

info

info attribute simply sets the information of the JSP page which is retrieved later by using getServletInfo() method of the Servlet interface.

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

<%@ page info="JSP tutorial by codebun.com" %>  
<!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>Insert title here</title>
</head>
<body>
<% 
out.println(getServletInfo());
%>
</body>
</html>

pageEncoding

The pageEncoding attribute specifies the character encoding for the JSP page. The Syntax for pageEncoding is:

<!-- Syntax  -->
<%@ page pageEncoding="any value"%>

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

session

The session attribute is present on the JSP page to manage the Session. Its value is either true or false. If it is set to false, then the JSP page will not maintain any session. By default, it is set to true. For example,

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

extends

This attribute is just like the keyword extend that we used to extend a class in Java. The Syntax for extends is

<%@ page extends="any value" %>

JSP include Directive

With the help of this directive, we can include the content of some JSP files on the current JSP page. Like for example, in a web application, we have some common content like header and footer. In order to avoid the duplication of this content on every JSP page, we can create one file and include it wherever is required.

For Example, the Syntax to include the header.jsp page is as follows:

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

Here, we will create the header.jsp file and also will include this file into main.jsp file.

header.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello!, I am a Header :-)
</body>
</html>

main.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ include file="header.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello, Welcome to Codebun</h1>
</body>
</html>

JSP Taglib Directive

Taglib is a Tag Library that contains many tags.

  • In JavaServer Pages, we can define user-defined tags called custom tags. We have to give the definition of the custom tags in a file called Tag Library Descriptor. (.tld file)
  • So, to refer to the tag library we use the taglib directive.

Syntax:

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

In this way, we learned what is Page, Include, and Taglib directives in JSP. In order to know more about this topic do check this article https://codedec.com/tutorials/what-are-jsp-directives/