JSP Directives

JSP Directives: Page, Include and TagLib

In this article, I am going to discuss JSP Directives: Page, Include, and TagLib. Please read our previous article where discussed JSP Elements. At the end of this article, you will understand the following pointers in detail.

  1. What are JSP Directives?
  2. Types of Directives in JSP
  3. The page Directive
  4. Attributes of JSP page directive
  5. Example of each JSP page directive Attributes
  6. Include Directive
  7. Example of the include directive
  8. The JSP Taglib Directive
  9. Example of JSP Taglib directive
What are JSP Directives?

Directives are basically wont to configure the code that’s generated by the container during a Servlet. JSP directives are JSP components that are used to give the instructions to the JSP compiler. JSP directives will simplify writing JSP files. These tags are used to give directions to the JSP page compiler. In web applications, JSP Directives can be used to define present JSP page characteristics, to include the target resource content into the present JSP page, and to make available user-defined tag library into this JSP page. All the JSP directives are going to be resolved at the time of translating the JSP page to the servlet. The majority of JSP Directives will not give a direct effect to response generation.

To provide JSP Directives in JSP pages we have to use the following syntaxes:
JSP-Based Syntax: <%@Directive_name[attribute-list]%>
Example: < %@page import=”java.io.*”%>
XML-Based Syntax: <jsp:directive.directiveName[attribute-list]%/>
Example: <jsp:directive.page import=”java.io.*”/>

Types of Directives in JSP:

There are three types of Directives in JSP technology:

  1. Page Directives
  2. Include Directives
  3. Taglib Directives
Page Directives in JSP

In JSP technology, page directives are often wont to define this JSP page characteristics wish to define import statements, specify particular superclass to the translated servlet, to specify metadata about present JSP pages, and so on. The JSP page directives define the properties for the entire JSP page by using its different attributes and set values of the attributes as per requirements. It is useful to provide global information for the JSP page. Basically, Page directives are used to give the instructions to the JSP compiler like what package to import, what language we are writing, etc. Page directives are basically used for supplying compile-time information to the container for generating a servlet. The page directive will take the info within the sort of (key, value) pair. Whenever we use page directive as a part of the JSP program that statement must be the first statement. The scope of the page directive is applicable to the current JSP page only. JSP program allows only one-page directive at a time.

Syntax: <%@ page attributeName=”values” %>

The space between the tag <%@ and %> before the page (directive name) and after values of the last attribute, is optional, you can leave the space or not.

Attributes of Page Directives

Following are the attributes of page directives used in JSP:

language:

Language is a page directive attribute that gives an instruction to the JSP compiler to use what kind of language inside the scriptlets. We can specify different languages like javascript, CSS, etc. By default, it takes java language inside the scriptlets. It is not recommended to use language attributes. Tomcat server or WebLogic server are not supporting this language attribute but resin server supporting this language attribute.

Example:
<%@ page language=”javascript”%>
<%
      var a=10;
      document.write(a);
%>

import:

import is page directives attribute that gives an instruction to the JSP compiler like what package to be imported into the JSP program. We can also import any number of packages using this import. But it is not recommended to import multiple packages using a single import attribute it is better to write multiple page directives with an import attribute to import multiple packages.

Example:
<%@ page import=”java.util.*”%>
<%
      ArrayList al = new arrayList();
      al.add(10);
      al.add(20);
      al.add(30);
%>
<%=al%>

info:

info is page directive attribute which give an instruction to JSP compiler to override getServletInfo() method to return the info about JSP file.

Example:
<%@ page info=”my servlet page”%>
<%
        out.println(getServletInfo());
%>

ContentType:

Content-Type is a page directive attribute that gives instructions to the JSP compiler like what type of content we are sending to the client. We can specify the content type like text/xml, text/css, etc.

Example:
<%@ page contectType=”text/xml”%>
<student>
<rno></rno>
<name></name>
</student>

buffer:

buffer is page directive attribute which give an instruction to JSP compiler what is the buffer size can be taken by out implicit variable of JspWriter (by default 8KB).

Example: <%@ page buffer=”5kb”%>

autoflush:

By default auto flush is true but if we want to control flushing on our own requirement we have to use this autoflush page directive.
Example :
<%@ page autoFlush=”false”%>
<%
    out.flush();
%>

isErrorPage:

isErrorPage is a directive attribute that is used to create the error page.

Example:
<%@ page isErrorPage=”true”%>
<html>
<head>
</head>
<body>
<%
     out.println(“Sorry division by zero is not possible”);
%>
</body>
</html>

errorPage:

If our JSP file containing any exception and if we want to display any error page then we can use this errorPage page directive attribute.

Example: <%@ page errorPage=”error.jsp”%>

session:

The session is a page directive attribute using which we can make the session enable or disable for a particular JSP page. By default, the session is true but if we want to make it false or true, we can use this session attribute. Once we set the session false on a particular page, we can control the session and its data.

Example: <%@ page session=”false”%>

extends:

extends is a page directive attribute using which we can create our JSP servlet program by extending from any other class.

Example: test.jsp

<%@ page extends=”sample.work.HtmlServlet”%>

Following servlet code will be generated when we execute the above JSP program:

public class test_jsp extends sample.work.Htmlservlet{}

isThreadSafe:

By default, every JSP page is true but if we want to make whether it is thread-safe or not, we have to mention isThreadSafe page directive attribute either to be true or false. True indicates multithreading and false indicates that the servlet should implement SingleThreadModel.

Example: <%@ page isThreadSafe=”true”%>

pageEncoding:

The pageEncoding is a page directive attribute using which we can specify what charset we are using in JSP.

Example: <%@ pageEncoding=”UTF-8″%>

isELIgnored:

isELIgnored is a page directive attribute used to define ENUM data type. Its default value is false.

Example: <%@ page isELIgnored=”false”%>

Example of Page Directives in JSP
<%@ 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>

Output

Example of Page Directives in JSP

Include Directives in JSP

Include Directive are often wont to include the content of the target resource into this JSP page. The include directive is employed to incorporate a file during the interpretation phase. This directive tells the container to merge the content of other external files with the present JSP during the interpretation phase. You may code include directives anywhere on your JSP page. It is used to include the code of destination program/file to the code of source JSP program’s JES class. This tag is given for code inclusion, not for output inclusion. This tag is given for code inclusion, not for output inclusion. Here, a separate JES class will not be generated for the destination file but the code of the destination file will be included in the JES class of the source file. This method does not use the rd.include(_,_) method internally. The include directive is employed to statically insert the contents of a resource into the present JSP. This enables a user to reuse the code without duplicating it and includes the contents of the required file at the interpretation time. This directive has just one attribute called a file that specifies the name of the file to be included. One JSP page can have more than one include directives.

Syntax: <%@ include file=”—”%>

Where file attribute can be used to specify the name and location of the target resource.

Example of Include Directive in JSP

logo.jsp
<%@ 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>
 <center>
  <table width="100%" height="20%" bgcolor="red">
   <tr>
    <td colspan="2"><center>
      <b><font size="7" color="white"> Hello World </font></b>
     </center></td>
   </tr>
  </table>
 </center>
</body>
</html>
body.jsp
<%@ 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 bgcolor="yellow">
 <center>
  <b><font size="7">
    <p>
     <br> Welcome!!!<br>
     <br>
    </p>
  </font></b>
 </center>
</body>
</html>
footer.jsp
<%@ 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>
 <center>
  <table width="100%" height="15%" bgcolor="blue">
   <tr>
    <td colspan="2"><center>
      <b><font size="6" color="white"> This is footer</font></b>
     </center></td>
   </tr>
  </table>
 </center>

</body>
</html>
mainpage.jsp
<%@ 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>
 <%@include file="logo.jsp"%>
 <%@include file="body.jsp"%>
 <%@include file="footer.jsp"%>
</body>
</html>

Output

Example of Include Directive in JSP

JSP Taglib Directives

The main purpose of Taglib Directives is to make available user-defined tag library into the present JSP pages. The JSP API allows you to define custom JSP tags that appear as if HTML or XML tags and a tag library may be a set of user-defined tags that implement custom behavior. The taglib directives declare that your JSP page uses a set of custom tags, identifies the location of the library, and provides a means for identifying the custom tags in your JSP page.

Syntax: <%@ taglib uri=”—” prefix=”—”%>

Where URI attribute can be used to specify the name and location of user-defined tag library and prefix attribute can be used to define prefix names for the custom tags.

Example of Taglib Directives in JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>JSP Taglib Directives</title>

</head>
<body>
 <c:out value="${'Welcome!!!'}" />
</body>
</html>

Output

Example of Taglib Directives in JSP

In the next article, I am going to discuss JSP Comments. Here, in this article, I try to explain the JSP Directives: Page, Include, and TagLib. I hope you enjoy this JSP – Directives: Page, Include, and TagLib article.

Leave a Reply

Your email address will not be published.