4716l1

Listing 1. HelloTag Tag Handler

package il.co.lerner;

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

public class HelloTag extends TagSupport {

   private String firstname = null; // field, with a null default

   // ------------------------------------------------------------
   // Bean-style "set" method 

   public void setFirstname (String newFirstname) {

       // Only set the firstname field if it's non-empty
       if (! newFirstname.equals(""))
            {
                firstname = newFirstname;
            }
   }

   // ------------------------------------------------------------

   public int doEndTag() throws JspException {

       // Insert some text inside of the JSP, or die trying
       try {

           // If no name was defined, print a generic message
           if (firstname == null)
                {
                   pageContext.getOut().println("Hi there!");
                }
           else
                {
                   pageContext.getOut().println("Hello, " + firstname);
                }

        } catch (IOException e) {
          throw new JspException(e.getMessage());
        }

        // Go ahead and evaluate the rest of the JSP
        return EVAL_PAGE;
    }

    // ------------------------------------------------------------

    public void release() 
    {
        firstname = null;
        super.release();
    }
    
} 

