Java JSP With A Custom Tag

HelloTag.jsp

To download source right-click here and "Save As...".

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);
    }
}
To download source right-click here and "Save As...".

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
Sign in to be the first to comment on Java JSP With A Custom Tag.