Complete Source Code Listings
Included Files: application.xml
|
<?xml version="1.0"?> <!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN" "http://java.sun.com/j2ee/dtds/application_1_2.dtd"> <application> <display-name>Tag Library Test Application</display-name> <description> A sample Tag Library Test Application </description> <module> <web> <web-uri>TagLib.war</web-uri> <context-root>/TagLib</context-root> </web> </module> </application> |
Included Files: web.xml
|
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'> <web-app> <display-name>Tag Library Test Application</display-name> <description> Web module that contains two JSP's. </description> <welcome-file-list> <welcome-file>untitled1.jsp</welcome-file> </welcome-file-list>
<taglib> <taglib-uri>/TagLib</taglib-uri> <taglib-location>/WEB-INF/TableDisplay.tld</taglib-location> </taglib>
</web-app> |
Included Files: TableDisplay.tld
|
<?xml version = '1.0' encoding = 'windows-1252'?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>TableDisplay</shortname> <uri>WEB-INF\TableDisplay</uri>
<info>A short description...</info>
<tag> <name>TableDisplayQuery</name> <tagclass>mypackage.TableDisplayQuery</tagclass> <bodycontent>JSP</bodycontent> <attribute> <name>dataSource</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>userName</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>password</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>JDBCClass</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>connectionURL</name> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>queryString</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
</taglib> |
|
<%@ page contentType="text/html;charset=windows-1252"%> <%@ taglib uri="WEB-INF/TableDisplay.tld" prefix="myTags"%> <myTags:TableDisplayQuery connectionURL="jdbc:rmi://localhost:1099/jdbc:cloudscape:CloudscapeDB;create=true" JDBCClass="COM.cloudscape.core.RmiJdbcDriver" userName="APP" queryString="SELECT * FROM USER_INFO" />
<!-- ------------------ --> <!-- THE
TAG LIBRARY -----> dataSource="jdbc/MoneyPlanDS" userName="APP" queryString="SELECT * FROM USER_INFO" />
<!-- ------------------ --> |
Complete
Java Source Code: TableTagUtils.java
|
package mypackage; import java.util.Collection; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import java.sql.ResultSet; import java.util.Collection; public class TableTagUtils { /** * displays the actual table * * @see #displayTitles * @param JspWriter out writer from the page the tag is in * @param ResultSet rs java.sql.ResultSet from the executed query * @param Collection t uses a collection of strings as the input * @exception NONE * @return String to be displayed */ protected static void writeTheOutput( JspWriter out, ResultSet rs, Collection t ) { try { out.print("<CENTER>"); out.print("<TABLE border='2' >"); out.print("<!-- heading row -->"); out.print( displayTitles(t) ); int colCount = t.size(); while ( rs.next() ){ out.write("<TR>"); for (int i = 0; i < colCount; i++) { out.write("<TD>" + rs.getObject(i+1).toString() + "</TD>"); } out.print("</TR>"); } } catch(Exception e) { e.printStackTrace(); }
finally { } /** * generates the string for the table.. this string is the column header * listing the names of all of the columns * * @see #generateColumnTitles * @see #writeTheOutput * @param Collection t uses a collection of strings as the input * @exception NONE * @return String to be displayed */ protected static String displayTitles( Collection t ) { StringBuffer b = new StringBuffer(); String d;
b.append("<TR>"); java.util.Iterator it = t.iterator();
while (it.hasNext()) { d = (String)it.next(); b.append("<TD><STRONG>" + d + "</STRONG></TD>"); } b.append("</TR>"); return b.toString(); } /** * Extract the column titles from the jdbc result set meta data, this is really * a helper function for the over all utility tag to quickly display results * of a query. * * @see #java.sql.ResultSetMetaData * @param ResultSet rs comments * @exception javax.servlet.jsp.JspException if ANY Error occurs * @return an ArrayList containing all of the colum names */ protected static Collection generateColumnTitles(ResultSet rs) throws javax.servlet.jsp.JspException { java.util.ArrayList columnTitles = new java.util.ArrayList(); try { java.sql.ResultSetMetaData rsMD = rs.getMetaData(); int x = rsMD.getColumnCount(); for (int i = 1; i <= x; i++) { columnTitles.add(rsMD.getColumnName(i)); } } catch ( Exception e ) { throw new javax.servlet.jsp.JspException("Could not get result set"); } return columnTitles; } protected static void checkStringVars( String s, String name ) throws Exception { if (( s == null ) || s.equals("")) { throw new javax.servlet.jsp.JspException("Atrtribute \"" + name + "\" cannot be blank or NULL!"); } } } |
Complete Java Source Code:
TableDisplayQuery.java
|
package mypackage; import javax.servlet.jsp.tagext.TagSupport; import javax.servlet.jsp.tagext.BodyTagSupport; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.servlet.ServletRequest; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.DriverManager; import java.util.Collection; import java.util.ArrayList; import mypackage.TableTagUtils.*; import javax.naming.InitialContext; import javax.naming.Context; import javax.rmi.PortableRemoteObject; public class TableDisplayQuery extends BodyTagSupport { private ResultSet resultSet; private Collection columnTitles; private String queryString; private javax.sql.DataSource ds; private java.sql.Connection conn; private String JDBCClass; private String userName; private String password; private String connectionURL; /** * Method called at start of tag. * @return EVAL_BODY_INCLUDE */ public int doStartTag() throws JspException { try { if ( ds == null ) { mypackage.TableTagUtils.checkStringVars(JDBCClass, "JDBCClass"); mypackage.TableTagUtils.checkStringVars(connectionURL,"connectionURL"); mypackage.TableTagUtils.checkStringVars(userName,"userName");
Class.forName(JDBCClass); conn = DriverManager.getConnection(connectionURL, userName, password); } else { conn = this.ds.getConnection(); }
resultSet = executeTheQuery(); if ( columnTitles == null ) columnTitles = mypackage.TableTagUtils.generateColumnTitles(resultSet); TableTagUtils.writeTheOutput( pageContext.getOut(), resultSet, columnTitles ); } catch (Exception e) { throw new javax.servlet.jsp.JspException(e.toString()); } finally { try { if (resultSet != null) resultSet.close(); } catch ( Exception e) {}; try { if (conn != null) conn.close();} catch ( Exception e) {}; }
return SKIP_BODY; } /** * Method called at end of tag. * @return EVAL_PAGE */ public int doEndTag() { return EVAL_PAGE; } /** * Method is invoked after every body evaluation to control whether the body will be reevaluated or not. * @return SKIP_BODY */ public int doAfterBody() throws JspException { return SKIP_BODY; } public void setQueryString( String qs ) { queryString = qs; } public void setJDBCClass( String className ) { JDBCClass = className; }
public void setUserName( String s ) { userName = s; } public void setPassword( String s ) { password = s; } public void setConnectionURL( String theURL ) { connectionURL = theURL; }
public void setDataSource( String dsName ) throws javax.servlet.jsp.JspException { try { Context ic = new InitialContext(); ds = (javax.sql.DataSource) ic.lookup(dsName); } catch (Exception e) { throw new javax.servlet.jsp.JspException(e.getMessage()); } }
private java.sql.ResultSet executeTheQuery() throws javax.servlet.jsp.JspException { java.sql.ResultSet results = null;
try { java.sql.Statement stmt = conn.createStatement(); results = stmt.executeQuery( this.queryString ); } catch (Exception e ) { throw new javax.servlet.jsp.JspException(e.getLocalizedMessage()); } return results; } } |
Complete
Java Source Code: TableDisplayRS.java
|
package mypackage; import javax.servlet.jsp.tagext.TagSupport; import javax.servlet.jsp.tagext.BodyTagSupport; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.servlet.ServletRequest; import java.io.PrintWriter; import java.sql.ResultSet; import java.util.Collection; import java.util.Collections; import java.util.ArrayList; import mypackage.TableTagUtils; public class TableDisplayRS extends BodyTagSupport { private ResultSet resultSet; private Collection columnTitles; /** * Method called at start of tag. * @return EVAL_BODY_INCLUDE */ public int doStartTag() throws JspException { try { JspWriter out = pageContext.getOut(); out.print("<CENTER>"); out.print("<TABLE border='2' >"); out.print("<!-- heading row -->"); if ( columnTitles == null ) columnTitles = TableTagUtils.generateColumnTitles(resultSet);
out.print( TableTagUtils.displayTitles(columnTitles) ); int colCount = columnTitles.size(); while ( resultSet.next() ){ out.write("<TR>"); for (int i = 0; i < colCount; i++) { out.write("<TD>" + resultSet.getObject(i+1).toString() + "</TD>"); } } } catch ( Exception e) { throw new JspException( e.getLocalizedMessage()); } return SKIP_BODY; } /** * Method called at end of tag. * @return EVAL_PAGE */ public int doEndTag() { return EVAL_PAGE; } /** * Method is invoked after every body evaluation to control whether the body will be reevaluated or not. * @return SKIP_BODY */ public int doAfterBody() throws JspException { return SKIP_BODY; } private ResultSet getResultSet() { return resultSet; } public void setResultSet(Object newResultSet) { resultSet = (ResultSet)newResultSet; } private Collection getColumnTitles() { return columnTitles; } public void setColumnTitles(Collection newColumnTitles) { columnTitles = newColumnTitles; }
} |