HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Writing Custom JSP Tag Libraries"
Vol. 5, Issue 10, p. 36

	


Listing 1

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

public class SimpleTag extends TagSupport {
  
public int doStartTag() {
  try {
    JspWriter out = pageContext.getOut();
    out.println("Java Developers Journal");
  } catch (IOException ioe) {
    System.out.println("Error writing to out: " + ioe);
  }
    return(SKIP_BODY);
  }
}



Listing 2

public class SimpleTag extends TagSupport {
  
  public int doStartTag() {
    try {
      JspWriter out = pageContext.getOut();
      out.println("<font face=geneva size=8  color=blue>");
    } catch (IOException ioe) {
      System.out.println("Error writing to out: " + ioe);
    }
      return(EVAL_BODY_INCLUDE);
    }

  public int doEndTag() {
    try {
      JspWriter out = pageContext.getOut();
      out.println("</font>");
    } catch (IOException ioe) {
      System.out.println("Error writing to out: " + ioe);
    }
    return(EVAL_PAGE);
  }
}

Listing 3

<%@ taglib uri="jdj.tld" prefix="jdj" %>

<html>
  <body>
    <jdj:simple>
     Custom JSP Tags!
    </jdj:simple>
  </body>
</html>

Listing 4

private String color;

//Set via the tag attribute fontcolor
public void setFontcolor(String value ) {
  color = value;
}

public int doStartTag() {
  try {
    JspWriter out = pageContext.getOut();
    out.println("<font face=geneva size=8 color="+color+">");
 } catch (IOException ioe) {
    System.out.println("Error writing to out: " + ioe);
  }
    return(EVAL_BODY_INCLUDE);
  }

Listing 5

package com.jdj;

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

public class UpperCaseTag extends BodyTagSupport {

public int doAfterBody() {
  try {
    BodyContent body = getBodyContent();
    JspWriter out = body.getEnclosingWriter();
    out.print( body.getString().toUpperCase() );
  } catch (IOException ioe) {
    System.out.println("Error writing to out: " + ioe);
  }
    return(SKIP_BODY);
  }

}

Listing 6

package com.jdj;

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

public class QueryTag extends BodyTagSupport {

  private ResultSet rs = null;
  private Statement st = null;
  private Connection con = null;

  //Attributes
  private String connectString;
  private String driverClass;
  private String sql;
  private String user;
  private String password;


  public int doStartTag() {
    loadData();
    return( EVAL_BODY_TAG );
  }

  public int doEndTag() {
    closeConnections();
    return( EVAL_PAGE );
  }

  private void closeConnections() {
    try { rs.close(); } catch ( Exception e ) {};
    try { st.close(); } catch ( Exception e ) {};
    try { con.close(); } catch ( Exception e ) {};
  }

  private void loadData() {
    try {
      Class.forName( driverClass );
      con = DriverManager.getConnection( connectString, user, password );
      st = con.createStatement();
      rs = st.executeQuery( sql );
      rs.next();
    } catch ( Exception e ) {
      System.out.println("Error loading data: " + e );
    }
  }

  public int doAfterBody() {
    try {
      BodyContent body = getBodyContent();
      JspWriter out = body.getEnclosingWriter();
      out.println( body.getString() );
      //Clear the body (in case we loop again)
      body.clearBody();
      if ( rs.next()  ) {
         //There is another row so evaluate the body again
        return( EVAL_BODY_TAG );
      } else {
        //Last row so don't evaluate the body anymore
        return( SKIP_BODY );
      }
    } catch ( Exception e ) {
      System.out.println( "Error in doAfterBody: " + e );
      return( SKIP_BODY );
    }
  }

  //This is the method our nested tags will call to get the
  //value of a particular column in the current row
  public String getDataValue( String columnName ) throws SQLException {
    return( rs.getString( columnName ) );
  }

  public void setConnectString( String value ) {
    connectString = value;
  }

  public void setDriver( String value ) {
    driverClass = value;
  }

  public void setQuery( String value ) {
    sql = value;
  }

  public void setUser( String value ) {
    user = value;
  }

  public void setPass( String value ) {
    password = value;
  }

}

Listing 7

package com.jdj;

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

public class DataValueTag extends TagSupport {

  //Attributes
  private String columnName = "";

  public DataValueTag() {
  }
  
  public int doStartTag() {
    try {
      JspWriter out = pageContext.getOut();
      //Now try to get the parent
      QueryTag qt = (QueryTag)findAncestorWithClass(this, QueryTag.class);
      if ( qt == null ) {
       out.print( "Must be enclosed in a query tag!");
      } else {
          try {
            String val = qt.getDataValue( columnName );
            out.print( val );
        } catch ( Exception e ) {
          System.out.println(" An error occurred in DataValueTag " + e );
          out.println("An error occurred: " + e );
        }
      }
    } catch ( Exception ioe ) {
      System.out.println( "Error in doStarTag" + ioe );
    }
    return( SKIP_BODY );
  }


  public void setColname( String value ) {
    columnName = value;
  }
}

Listing 8

<tag>
  <name>query</name>
  <tagclass>com.jdj.QueryTag</tagclass>
  <bodycontent>JSP</bodycontent>
  <attribute>
    <name>sql</name>
    <required>true</required>
  </attribute>
  <attribute>
    <name>driver</name>
    <required>false</required>
  </attribute>
  <attribute>
    <name>connectString</name>
    <required>false</required>
  </attribute>
  <attribute>
    <name>user</name>
    <required>true</required>
  </attribute>
  <attribute>
    <name>pass</name>
    <required>true</required>
  </attribute>
</tag>

<tag>
  <name>data_value</name>
  <tagclass>com.jdj.DataValueTag</tagclass>
  <bodycontent>EMPTY</bodycontent>
  <attribute>
    <name>colname</name>
    <required>true</required>
  </attribute>
</tag>

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.