Here is the code that I use

Notes:
1) I have a ConnectionPool object which is a Singleton. Just replace the
ConnectionPool stuff
with your own code to connect to your DB

2) One bad side effect of this implementation is that the JSP page (view)
contains fields and
table names from the database, so to some extent it is coupled to the
database design. This could
be avoided by using an XML file that matches values from the JSP file up to
real table and field
names in the database.

3) Of course this code is a work-in-progess. Use and modify as needed. It
mostly works.

Here is the XML for the tag library

<taglib>
        <tlibversion>1.0</tlibversion>
        <jspversion>1.1</jspversion>
        <shortname>ASAT Tags</shortname>
        <tag>
                <name>optionlist</name>
                <tagclass>OptionListTag</tagclass>
                <bodycontent>JSP</bodycontent>
                <info>Builds an option list from a database table</info>
                <attribute>
        <name>name</name>
              <required>true</required>
           <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
        <name>size</name>
              <required>true</required>
           <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
        <name>multiple</name>
              <required>false</required>
           <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
        <name>datacolumn</name>
              <required>true</required>
           <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
        <name>displaycolumn</name>
              <required>true</required>
           <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
        <name>table</name>
              <required>true</required>
           <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
        <name>whereclause</name>
              <required>false</required>
           <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
        <name>showdata</name>
              <required>false</required>
           <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
        <name>maxwidth</name>
              <required>false</required>
           <rtexprvalue>true</rtexprvalue>
                </attribute>
        </tag>
</taglib>

Here is the source for my tag

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

/**
 * Forms an HTML SELECT tag given the following parameters
 * name - name of the SELECT tag in the form
 * size - number of options to show
 * multiple - (optional) is multiple selection on?
 * datacolumn - The value to return when an item is selected
 * displaycolumn - The value to show to the user in the list. The list is
sorted on this value.
 * table - The database table where all of the data can be found
 * whereclause - (optional) where clause
*/

public class OptionListTag extends TagSupport {
   private String name;
   private String size;
   private String multiple;
   private String datacolumn;
   private String displaycolumn;
   private String table;
   private String whereclause = null;
   private String showdata = "no";
   private String maxwidth;
   private int maximumWidth = 0;

   public void setName(String newName) {
      name = newName;
   }

   public void setSize(String newSize) {
      size = newSize;
   }

   public void setMultiple(String newMultiple) {
      multiple = newMultiple;
   }

   public void setDatacolumn(String newDatacolumn) {
      datacolumn = newDatacolumn;
   }

   public void setDisplaycolumn(String newDisplaycolumn) {
      displaycolumn = newDisplaycolumn;
   }

   public void setTable(String newTable) {
      table = newTable;
   }

   public int doStartTag() throws JspException
   {
      ConnectionPool pool = ConnectionPool.getPool();
      Connection con = null;

      StringBuffer html = new StringBuffer();

      html.append("<select name=\"");
      html.append(name);
      html.append("\" size=\"");
      html.append(size);
      html.append("\"");
      if (multiple.compareToIgnoreCase("yes") == 0)
      {
         html.append(" multiple>\n");
      }
      else
      {
         html.append(">\n");
      }
      try
      {
         con = pool.getConnection();
         //
         Statement stmt = con.createStatement();
         StringBuffer sql = new StringBuffer();

         sql.append("select \"" + datacolumn + "\", \"" + displaycolumn +
"\" from \"" + table + "\"");
         if (whereclause != null)
         {
            if (whereclause.length() != 0)
            {
               sql.append(" WHERE " + whereclause);
            }
         }
         sql.append(" order by \"" + displaycolumn + "\"");
         ResultSet rs = stmt.executeQuery(sql.toString());
         //
         while (rs.next())
         {
            String aData, aDisp;
            aDisp = rs.getString(1);
            aData = rs.getString(2);
            if (maximumWidth > 0)
            {
               if (aData.length() > maximumWidth)
               {
                  aData = aData.substring(0,maximumWidth);
               }
            }
            html.append("<option value=\"");
            html.append(aDisp);
            html.append("\">");

            if (showdata.compareToIgnoreCase("yes") == 0)
            {
               html.append("(");
               html.append(aDisp);
               html.append(") ");
               html.append(aData);
            }
            else
            {
               html.append(aData);
            }
            html.append("</option>\n");
         }
         rs.close();
         stmt.close();
         pool.returnConnection(con);
      }
      catch (SQLException se)
      {
         System.out.println(se);
         throw new JspException("Database Error " + se.getMessage());
      }
      finally
      {
         if (con != null) pool.returnConnection(con);
      }
      // Write out the rest of the HTML
      html.append("</select>\n");

      try
      {
         pageContext.getOut().write(html.toString());
      }
      catch (IOException ioe)
      {
         System.out.println(ioe);
         throw new JspException(ioe.getMessage());
      }
      return EVAL_BODY_INCLUDE;
   }

   public void setWhereclause(String newWhereclause) {
      whereclause = newWhereclause;
   }

   public void setShowdata(String newShowdata) {
      showdata = newShowdata;
   }

   public void setMaxwidth(String newMaxwidth) {
      maxwidth = newMaxwidth;
      maximumWidth = (new Integer(maxwidth).intValue());
   }
}

Dave Bolt
ATSC/SPAWAR ASAT Team
Bolt's Law of Bandwidth - There is always plenty of network bandwidth, just
none for you.


-----Original Message-----
From: Jurrius, Mark [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, September 12, 2000 11:38 AM
To: [EMAIL PROTECTED]
Subject: For-Loop Custom Tag


Can anybody provide me with an example or point me in the right direction as
to how I can make a custom tag that runs a SQL query where it populates an
HTML SELECT-OPTION list using a FOR loop and one of the options might be
selected?

Thanks in advance.

Mark

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to