Directives in JSP

In this article, we will see the list of JSP directives.

First of all, jsp directives are kind of messages that tells the web container how to translate the web page to a servlet.

They are:

a)@page b)@include c)@taglib

Let’s explore each directive in detail.

  1. @page: In this directive, the attributes are defined which are applicable to the whole page.So whenever we are accessing those attributes at any part of the page, it would be accessible. It is declared as follows.

<%@ page attribute=”value” %>  

Which attributes are available?

  • import
  • contentType
  • extends
  • info
  • buffer
  • language
  • isELIgnored
  • isThreadSafe
  • autoFlush
  • session
  • pageEncoding
  • errorPage
  • isErrorPage

Let’s look into few of them.

import : The import attribute is used with @page directive and it is similar to the one which we have java classes. It is used to import a class,interface or all members of a particular package. In the below jsp, Date class is imported and used to print the date.
<html>  
<body>  
  
<%@ page import=”java.util.Date” %>  
Today is: <%= new Date() %>  
  
</body>  
</html>  

extends:It mentions the name of the class which would be extended in the servlet. As discussed initially above, the jsp would be translated into servlet when execution happens. So that servlet would extends that class which would be mentioned in the @page directive’s attribute.

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

<html>  
<body>  
  
<%@ page info="Tech Finance World" %>  
Today is: <%= new java.util.Date() %>  
  
</body>  
</html>

errorPage: This attribute sets the error page to which the request would be forwarded if an exception occurs in any scriptlet inside the jsp. Let's look at the below implementation
<html>  
<body>  
<%@ page errorPage="errorPageTest.jsp" %>
<b>Let's throw an divide by zero exception</b>  
 <%= 10/0 %>  
</body>  
</html>  

Once the exception occurs above, it is directed to the errorPageTest.jsp

2) Include:

Include directive is used to inlcude other resources in the same jsp page.Consider a jsp sample.jsp

<%@ include file=”sample2.jsp” %> 

Here, the contents of sample2.jsp would be included in the sample.jsp

That normally is an header file which the jsp’s include which has the logic to build or show the menus.

3) taglib: The taglib directive in a jsp is used to define a tag library that defines many tags. We use the TLD (Tag Library Descriptor) file to define the tags.

Let’s look at an example.

<html>  
<body>  
  
<%@ taglib uri="http://www.tfw.com/tags" prefix="custtag" %>  
  
<custtag:lightner/>  
  
</body>  
</html>