Thursday, February 24, 2011

How to write a custom JSP tag ?

JSP tag is a very useful syntax of JSP. Using this syntax  we can define some operations.
When the JSP's executing the web container will invoke those operations.
JSP tags will help us to remove bulk codes from JSP files (Java and JavaScript ,..).
The most important thing is the tag re-usability.

JSP tag should have a start tag , end tag and tag body is optional.

If you want to use some UI part repeatedly in your project, without duplicating the code you can define your own tag.

If you want to write your own tag (custom tag) then you have to do two things.

1) Write a Java class to define the functionality of tag.

2) Write TLD file, which will describe the above Java class and how your tag will be called.

Creating the TLD file

Later we will call this file as a sample.tld (extension should be .tld).
You need to include this TAG file(sample.tld) to WEB-INf directory or it's subdirectory of your project.
<!--?xml version="1.0" encoding="ISO-8859-1" ?-->  
    <info>Sample tag library</info>  
        <!--Full qualified class name-->     
        <info>A tag for displaying reporting ui</info>  
        <!--You can define any number of attribute -->  
         <!--according to your need -->  

    Creating the Java Class

You need to write a java class extending the BodyTagSupport class.
 * used to generate reporting UI 
public class Report extends BodyTagSupport {  
 private boolean pdfReport;  
 private boolean htmlReport;  
 private boolean excelReport;  
 public boolean isPdfReport() {  
    return pdfReport;  
 public void setPdfReport(boolean pdfReport) {  
    this.pdfReport = pdfReport;  
 public boolean isHtmlReport() {  
    return htmlReport;  
 public void setHtmlReport(boolean htmlReport) {  
    this.htmlReport = htmlReport;  
 public boolean isExcelReport() {  
    return excelReport;  
 public void setExcelReport(boolean excelReport) {  
    this.excelReport = excelReport;  
 public int doStartTag() throws JspException {  
  JspWriter writer = pageContext.getOut();  
        String context = "<div>";  
        if (pdfReport) {  
            context = context + "<a style="background-image:url(../admin/images/pdficon.gif);">Generate Pdf Report</a>";  
        if (htmlReport) {  
            context = context + "<a style="background-image:url(../admin/images/htmlicon.gif);">Generate Html Report</a>";  
        if (excelReport) {  
            context = context + "<a style="background-image:url(../admin/images/excelicon.gif);">Generate Excel Report</a>";  
        context = context + "</div>";  
        try {  
        } catch (IOException e) {  
          String msg = "Cannot write reporting tag content";  
            throw new JspException(msg, e);  
        return EVAL_PAGE;  
Now you have to include following line in your JSP before use this custom tag.
<%@ taglib uri="http://myproject/taglibs.jar" prefix="carbon" %>
Now you can use new custom tag in your JSP like this.
Finally reporting UI generate like this.
Let's say if you want to add only PDF icon, then you can call custom tag like this
without changing Java code , JavaScript code, or HTML.
<carbon:report pdfreport="true" htmlreport="false" excelreport="false">