Java JSP With A Custom Tag

HelloTag.jsp

download source, use right-click and "Save Target As..." to save with a .jsp extension.

HelloTag.java

package tags;  //note: must be in a package

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class HelloTag extends TagSupport {
    public int doStartTag() {
        try {
            //use JspWriter to get output to your JSP
            JspWriter jspWriterOutput = pageContext.getOut();
            jspWriterOutput.print("Hello Tag!");
        } catch (IOException ioEx) {
            System.out.println("IOException in HelloTag " + ioEx);
        }
        return (SKIP_BODY);
    }
}
download source, use right-click and "Save Target As..." to save with a .java extension.

helloTag.tld

Notes

The helloTag I show was generated by NetBeans, and then I modified it just a little. I generally like to code my own XML files, but even the Sun tutorial on custom tags suggests letting your ide do it.

The tld files can be put directly into WEB-INF. I accidentally put mine in the directory with the tag class file, and I now prefer it that way.

References


online
The Java 2 Platform Specification
Books
Core JSP by Damon Hougland and Aaron Tavistock
Core Servlets and JavaServer Pages by Marty Hall
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by ramesh on 2008-05-12 Rate this Comment

this will be very usefull for beginners