[ Team LiB ] Previous Section Next Section

Using Custom Tags to Define Scripting Variables

Custom tags may also define scripting variables to use within the JSP page. Scripting variables are used in processing JSP page content, just as variables would be in any Java implementation. The scripting variables can be referenced the same as JavaBeans: by using the getAttribute() and setAttribute() methods. To create a scripting variable, implement a class that extends the TagExtraInfo class as shown in the GreetingsTei.java file in Listing 18.6.

Listing 18.6 GreetingsTei.java
package com.tags;

import javax.servlet.jsp.tagext.*;

public class GreetingsTei extends TagExtraInfo
{

  public GreetingsTei() {}

  public VariableInfo[] getVariableInfo(TagData data) {
     return new VariableInfo[] {
           new VariableInfo("bidderId","String",true, VariableInfo.AT_BEGIN),
           new VariableInfo("accountStatus","String",true, VariableInfo.AT_BEGIN)
    };
  }
}

Next implement initialization and variable modification logic within the parent tag handler class, as shown in Listing 18.7, GreetingsBidder.java.

Listing 18.7 GreetingsBidder.java
package com.tags;

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

public class GreetingsBidder extends TagSupport
{
  public GreetingsBidder() {}

  public int doStartTag() throws JspException  {
      try {

         HttpSession session = pageContext.getSession();
         String bidder = "NOT SET";
         String actStat = "NOT SET";

         if (null != session.getAttribute("bidderId")) {bidder = (String)session
graphics/ccc.gif.getAttribute("bidderId");}

         if (null != session.getAttribute("accountStatus")) {actStat = (String)session
graphics/ccc.gif.getAttribute("accountStatus");}

         pageContext.setAttribute("bidderId", bidder);
         pageContext.setAttribute("accountStatus",actStat);
         pageContext.getOut().println("<h1>Greetings Bidder Number: " + pageContext
graphics/ccc.gif.getAttribute("bidderId") + "</h1>");
         pageContext.getOut().println("<h1>Your Account is " + pageContext.getAttribute
graphics/ccc.gif("accountStatus") + "</h1>");
      }
      catch (Exception ex) {
         throw new JspException("GreetingsBidder Tag:Exception: " + ex.getMessage());
      }
      return SKIP_BODY;
   }
   public int doEndTag() {return EVAL_PAGE;}
}

Next define the tag handler (if not done previously) and tag extra info class within the TLD as shown here:

  <name>greetingsBidder</name>
  <tag-class>com.tags.GreetingsBidder</tag-class>
  <tei-class>com.tags.GreetingsTei</tei-class>
  <bodycontent>empty</bodycontent>
  <description>Displays Bidder Greeting</description>
 </tag>

Jar the TLD and subject classes (or add new classes and an updated TLD to an existing tag library JAR file—you can add new tags to existing tag libraries). Register the resultant JAR (if not previously registered) within the target application's web.xml as discussed earlier. Finally, you need only code a JSP that uses the scripting variables as shown in Listing 18.8, GreetingsBidder.jsp.

Listing 18.8 GreetingsBidder.jsp
<%@ taglib uri="greetings.tld" prefix="greet" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Bidder Greetings</title>
</head>

<body><table>
<tr><td><greet:greetingsBidder /></td></tr>

    <% session.setAttribute("bidderId","1001");
      session.setAttribute("accountStatus","VALID");
    %>

<tr><td><greet:greetingsBidder /></td></tr>
</body>

The browser view produced by GreetingsBidder.jsp is presented in Figure 18.5.

Figure 18.5. Browser view of GreetingsBidder.jsp.

graphics/18fig05.gif

    [ Team LiB ] Previous Section Next Section