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"
         extends="classA"
         
import="classP{,+}"
        
 session="true|false"
      
   buffer="none|sizeInKB"
  
       autoFlush="true|false"
        
 isThreadSafe="true|false"
       
  info="text"
      
   errorPage="jspUrl"
      
   isErrorPage="true|false"
       
  contentType="mimeType{;charset=charset}"
     %> 

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> http://www.sbi.com/Tags </taglib-uri>
            <taglib-location> /sbibank/account/</taglib-location>
            <taglib>

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

Login.jsp:

<html>
<body bgcolor="cyan">
<pre>
<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"/>
</form>
</pre>
</body>
</html>
 

Loginprocess.jsp:

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

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:directive");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from userdetails");

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

<%@include file="/login.jsp"%>
<%
return;
}}
finally{
try{con.close();}
catch(Exception e){}
}
%>
This is a home Page<br>
welcome<%=uname%>
</body></html>

Error.jsp:

<%@page isErrorPage="true" %>
<html>
<body>
An exception is raised during request:<br/>
<b>
Exception: </b><br/>
&nbsp:&nbsp:&nbsp:&nbsp:&nbsp:&nbsp:<%=exception.getMessage()%>
</b></br>
</body>
</html>

OUTPUT:

Login.jsp:

login.gif

LoginProcess.jsp:

   a. loginSuccess:

loginprocess.gif

   b. loginInvalid:

Loginprocessinvalid.gif