An overview of JSP directive Tag

Directive Tags in JSP:

Directive tags are used to give directions used by the JSP translator during the translation stage of the JSP life cycle.Directive tag allows the user to import packages, define error handling pages or session information of JSP page. These tags are used to set global values such as class declarations, methods to be implemented, output content type and so on. Thus we can say that the directive tag provides additional information to the JSP Engine regarding the jsp page.

Types of Directive Tags:

According to the JSP specification, there are mainly three directive tags are available:

  1. page
  2. include
  3. taglib

The Page Directive Tag: The page directive tag holds the instruction used by the translator during the translator stage of  the JSP life cycle. You can use the directive tags multiple times in a JSP page. The page directive used in any part of the JSP page. Thus we can say that, page directive sets page-level preferences for the JSP. Here is the syntax for the page directive:

         <%@page attributes%>

 E.g.:  <%@page language="java" %> 

Attributes for it is:

     <%@ page language="java"

The Include Directive: The include directive tag is used to merge the contents of two or more files during the translation stage of the JSP lifecycle. It also allows the developer to include other JSP or static page when JSP is translated into a Servlet, while the action <jsp:include> includes the resources at request time. The syntax for the include directive:

          <%@include file= "file path"%>

 E.g.:   <%@include file="/portal/login.jsp"%> 

The Taglib directive: The taglib directive tag is used to declare a custom tag library in a JSP page so that the tags related to that custom tag library can be used in the same JSP page. The taglib directive enables you to create your own custom tags. The syntax for it is:

           <%@taglib uri="URI"prefix="unique_prefix " %> 

 E.g.:   <taglib>
           <taglib-uri> </taglib-uri>
            <taglib-location> /sbibank/account/</taglib-location>

An Application of these Directive tags: There is an example in which I will try to describe the application of  directive tag:


<body bgcolor="cyan">
<form action="loginprocess.jsp">
<b>User Name</b> : <input type="text" name="uname"/>
<b>word</b> : <input type="text" name=""/>
<input type="submit" value="Login"/>


<%@page import="java.sql.*" errorPage="myerror.jsp" %>
<body bgcolor="pink">
Connection con=null;
String uname=request.getParameter("uname");
String =request.getParameter("");

Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from userdetails");

User Details For uname:<%=request.getParameter("uname")%>are not valid<br/>Try again

<%@include file="/login.jsp"%>
catch(Exception e){}
This is a home Page<br>


<%@page isErrorPage="true" %>
An exception is raised during request:<br/>
Exception: </b><br/>





   a. loginSuccess:


   b. loginInvalid:


Similar Articles