<s1 title="Tag Attributes">

<p>Tags become more useful once you add attributes.  This example creates
a <var/ct:href/> tag.</p>

<p>With session rewriting, even browsers without
cookies can support sessions.  However, every <var/&lt;a href>/> needs a
call to <var/response.encodeURL()/>, which is annoying.  With the
<var/ct:href/> tag, you get that for free.</p>

<s2 title="Using an attribute taglib">

<example title="href.jsp">
&lt;%@ taglib prefix="ct" uri="WEB-INF/tags.tld" %>
Message: &lt;ct:href href="test.jsp">test link&lt;/ct:href>
</example>
<results>
Message: &lt;a href="test.jsp;jsessionid=1234">test link&lt;/a>
</results>

</s2>

<s2 title="Defining the tag">

<p>The href tag has a single attribute <var/href/>.  Corresponding to the
attribute <var/href/>, the HrefTag class
implements <var/getHref()/> and <var/setHref()/> methods.</p>

<example title="HrefTag.java">
package test;

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

public class HrefTag extends TagSupport  {
  private String href;

  public String getHref()
  {
    return href;
  }

  public void setHref(String href)
  {
    this.href = href;
  }

  public int doStartTag() throws JspException
  {
    try {
      JspWriter out = pageContext.getOut();
      HttpServletResponse response;
      response = (HttpServletResponse) pageContext.getResponse();

      out.print("&lt;a href=\"");
      out.print(response.encodeURL(href));
      out.print("\">");
    } catch (IOException e) {
    }
    
    return EVAL_BODY;
  }

  public int doEndTag() throws JspException
  {
    try {
      pageContext.getOut().print("&lt;/a>");
    } catch (IOException e) {
    }
    
    return EVAL_PAGE;
  }
}
</example>

</s2>

<s2 title="Attribute Configuration">

<p>Each expected attribute needs an entry in the .tld.  In this case,
<var/href/> is a required attribute, so the configuration needs to
set <var/required/>.  By setting <var/required/>, the JSP parser can
detect errors at parse-time, making it easier to develop JSP pages with
the tag library.</p>

<example title="WEB-INF/tag.tld">
&lt;taglib>
  &lt;tag>
    &lt;name>href&lt;/name>
    &lt;tagclass>test.HrefTag&lt;/tagclass>
    &lt;attribute>
      &lt;name>href&lt;/name>
      &lt;required>true&lt;/required>
    &lt;/attribute>
  &lt;/tag>
&lt;/taglib>
</example>

</s2>
</s1>
