Tag Attributes
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Servlets and Filters
Portlets
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Introduction
JSP Compilation
Reference
Tutorials
Articles
FAQ

JSP Page Creation
Request
Topics
Tag Libraries

Tag Reuse
Hello Tag
Tag Attributes
Mail Tag
Tag Files
Hello Tag
Tag Libraries
Mail Tag

Tags become more useful once you add attributes. This example creates a ct:href tag.

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

  1. Using an attribute taglib
  2. Defining the tag
  3. Attribute Configuration

Using an attribute taglib

href.jsp
<%@ taglib prefix="ct" uri="WEB-INF/tags.tld" %>
Message: <ct:href href="test.jsp">test link</ct:href>

Message: <a href="test.jsp;jsessionid=1234">test link</a>

Defining the tag

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

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("<a href=\"");
      out.print(response.encodeURL(href));
      out.print("\">");
    } catch (IOException e) {
    }
    
    return EVAL_BODY;
  }

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

Attribute Configuration

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

WEB-INF/tag.tld
<taglib>
  <tag>
    <name>href</name>
    <tagclass>test.HrefTag</tagclass>
    <attribute>
      <name>href</name>
      <required>true</required>
    </attribute>
  </tag>
</taglib>


Hello Tag
Tag Libraries
Mail Tag
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.