JSP Taglib Directive Element

JSP taglib Directive elements

  • JSP Directive elements are used in the JSP file and provide information to the JSP container about the page. There are three directives available: page, include, and taglib.
  • The taglib directive specify libraries of tag files we will use in the current JSP file. This can either be a Custom JSP Tag Library or JSP Standard Tag Library (JSTL).
  • The beauty of the tag file is that you can invoke them with attributes from within JSP pages and get some results back.
  • You may code many taglib directives anywhere in your JSP page, but the taglib directives are normally coded at the top of the JSP page.
  • JSP taglib directive elements are specified in the form of a JSP style notations (not in XML style like other directive elements).
    Here is the notation for the taglib directive element:
    JSP Style Description
    <%@ taglib attributes %> Declares a custom tag library or a Standard Tag Library to use in this page.
  • The following table lists the attribute(s) for the page directive:
    Attribute Valid values Description
    prefix A char. or short string of characters This attribute tells the container that all tags of the associated tag library will be prefixed by the value of this attribute when used within the current JSP (ex.: <c:forEach>).
    tagdir /WEB-INF/tags ... This attribute tells the container to look in the specified directory to find the tag files implementation for a tag library. This attribute must contain a path that begins with /WEB-INF/tags. Typically used by Custom Tag Library.
    uri Absolute or relative uri. This is a URI that will be used by the container to locate a TLD file that describes the tag library. Typically used by Java Standard Tag Library.

Example of using JSP taglib directive element.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is TaglibDirective).

  • We will start by creating a customer tag file. This file must be placed in the directory, /WEB-INF/tags.
    Here is our customer tag file, getData.tag :
    <%@tag  pageEncoding="UTF-8"%>
    
    <%-- The list of normal or fragment attributes can be specified here: --%>
    <%@attribute name="operand1" required="true" type="java.lang.Integer" %>
    <%@attribute name="operand2" required="true" type="java.lang.Integer" %>
    
    <%-- any content can be specified here e.g.: --%>
    ${operand1*operand2}

    For those who participate in the review: create a tag file in Netbeans and replace generated code for the tag file with that shown above (the tag file name is getData).

  • Then we need a JSP file to demonstrate how we use the taglib directives.
    Here is the main JSP file:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="info" tagdir="/WEB-INF/tags" %> 
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
      </head> 
        <h1>Hello World!</h1>
        I have got a garden of  
        <info:getData  operand1="10" operand2="5"/> m<sup>2</sup>.
    </html>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Presentation.jsp ).

How does this work?

  • The tag file, getData.tag, contain two attributes that work as input for the JSP file when it invokes the tag file.
  • The data of the attributes is used in a multiplication, and the result is printed using an EL expression
  • We are indicating at the start of JSP file where to find a tag library we want to use through the taglib statement.
  • With a tag containing the prefix and tagfile name (<info:getdata) we deliver the to attributes (operand1 and operand2) to the tag file which write on place the result of an multiplication.

Creating Deployment descriptor.

  • To run this JSP you have to deploy it to a web-server or a Application server. To deploy means to install the JSP with some instruction to a such server.
  • The instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.

    You may need to create a Deployment descriptor file, web.xml in Netbeans.

  • The contents of the web.xml file should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
        <servlet>
            <servlet-name>Presentation</servlet-name>
            <jsp-file>/Presentation.jsp</jsp-file>
        </servlet>
        <servlet-mapping>
            <servlet-name>Presentation</servlet-name>
            <url-pattern>/Presentation</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>Presentation</welcome-file>
        </welcome-file-list>
    </web-app>
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a JSP file.
  • With a servlet tag we give the JSP file a servlet name, which is used in the servlet-mapping tag to specify a url for the JSP file.
  • In this way we can have many urls for the same JSP file.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the welcome file with url Presentation. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /TaglibDirective) for the application will in most cases be specified by a server vendor deployment descriptor.

    For those who participate in the review: create a Glassfish deployment descriptor in Netbeans.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, 
    Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" 
    "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
    <sun-web-app error-url="">
      <context-root>/TaglibDirective</context-root>
      <class-loader delegate="true"/>
      <jsp-config>
        <property name="keepgenerated" value="true">
          <description>
            Keep a copy of the generated servlet class' java code.
          </description>
        </property>
      </jsp-config>
    </sun-web-app>

Run the application.

  • For those who participate in the review: right click the Web-project and select Run.
  • This results in that the application will be deployed to the server and started.
  • The browser will display:
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.