Java Tutorials

Core Java
JSP
Servlet
JDBC
Hibernate
Struts 1
Struts 2
JSF
Spring
J2EE
J2ME
Web Services
Ajax
Dojo
MySQL
Latest Comments
user understand pr
Struts iterator/se
Java Sms
I will Appreciate
web services
  All Comments...
 

 

 
Struts Tutorials
*Stuts TOC
*Apache Struts Introduction
* Struts Controller
* Struts Action Class
* Struts ActionFrom Class
* Using Struts HTML Tags
*Struts Validator Framework    
*Client Side Address Validation    
*Struts Tiles
*tiles-defs.xml
*Struts DynaActionForm
*Struts File Upload
*Struts DataSource
*AGGREGATING ACTIONS
*Internationalization
Struts Resources
*Struts Books
*Struts Articles
*Struts Frameworks
*Struts IDE
*Struts Alternative
*Struts Links
*Struts Presentations
*Struts Projects
*Struts Software
*Struts Reference
*Struts Resources
*Other Struts Tutorial
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

JSP TAG LIBRARIES

                          

JSP’s offer a unique feature of “Tag Libraries”. Simply put, these are custom defined JSP tags. They are basically meant for componentizing presentation level logic. They are very similar to beans except the fact that Beans are used to separate Business logic.

Every tag is mapped to a particular class file which is executed whenever the tag is encountered in an JSP file. 

The general syntax :

<%@  taglib uri=“ - - - - - ” prefix = “ - - - ” %>  

The parent of the tag is the JSP tag within which it is nested, if there isn’t one, its set to null.

There are two things to remember while using TAGLIB’s :

The Class file should be created and it should be deployed in the Servlet folder of the web server. The class file should implement the Tag and BodyTag interfaces.

The mapping of a particular tag to a particular class file should be done in the  taglib.tld file.  

The tag is then ready to be used in the JSP file !!

The taglib file forms the central description for the tag library :

This file is an XML document that defines the tag’s operation.

It maps the tag name on the page  to the implementing class.

It defines inputs to the class.

It references the helper class that provides details of any output variables set by the tag class.

This Tag and Bodytag have some methods, these all methods are callback methods.

Tag methods: doStartTag() 
                         
  
doEndTag()

Body Tag       doAfterBody()

Let’s have small example to display Hello World :

1. First we write simple jsp file

     <% @ taglib uri=”/taglib.tld”  prefix=”nt” %>

     <html>

     <body>

               <nt:Helloworld />

     </body>

     </html>

 

2. It will look for file specified in uri attribute,that file will be  

   <?xml version=”1.0” encoding=”ISO-8859-1” ?>

<taglib>

      <tlibversion>1.0</tlibversion>

      <jspversion>1.1</jspversion>

      <shortname>nt</shortname>

          <tag>

                    <name>Hello World</name>

                    <tagclass>mytags.HelloWorld</tagclass>

                    <body context>empty</body context>

          </tag>

</taglib>

 

  3.  Next this will look for Helloworld .class file will be

   

Package mytags;

Import javax.servlet.jsp.*;  
Import javax.servlet.jsp.tagtext.*;
 

Public class Helloworld implements Tag {

          private  PageContext pagecontext; 
         
private Tag parent;

          public   int doStartTag() throws JSPException { 
                   
return SKIP_BODY; 
         
}

          public int doEndTag () throws JSPException   { 
                   
try{  
                   
          pageContext.getOut().write(“Hello World”);  
                   
     }  catch(java.io.Exception ex)   {  
                                 throw new JSPException(“IO Exception”);  
                      
       }  
                 
          return EVAL_PAGE;  
            }

  public void release (){}  

public void setPageContext(pageContext p) { 
         
pageContext=p;  
      }

public void setParent(Tag t) { 
         
parent = t;  
    }

public void getParent() {  
         return parent;  
    }  

}

Flow of programme:

            Here in first .jsp file we are writing tag.But name and properties of this tag we are writing in .tld file. And whatever task that tag is going to perform is written in .class file which has been called by .tld file.

          So by using taglib we can create our own defined tags.  

Author: Hrishikesh Deshpande

                          

Useful Links
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.