How can I set variables from within a custom tag and make them available to the including JSP page?

Jorge Jordão

In 3 simple (and simplified) steps

  • Create a TagExtraInfo subclass, defining the names and types of variables in the return value of the getVariableInfo(TagData) method
    public class MyTagExtraInfo extends TagExtraInfo {
        public VariableInfo[] getVariableInfo(TagData data) {
            return new VariableInfo[] {
                new VariableInfo("name", "java.lang.String", true, VariableInfo.NESTED)
  • In the tag handler class, add the variables to the PageContext as attributes
    public class MyTagHandler extends extends TagSupport {
        public int doStartTag() throws JspTagException {
            pageContext.setAttribute("name", "Duke");     
  • In the TLD, add a <teiclass> tag specifying the TagExtraInfo subclass

Find out more in Chapter 12 of Professional Java Server Programming J2EE Edition available online at java.sun.com.