---WEB-INF\mytaglib.tld
<?xml version="1.0" encoding="UTF-8" ?>
<!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>
<!-- ============== Tag Library Description Elements ============= -->
<tlibversion>1.2</tlibversion>
<jspversion>1.1</jspversion>
<shortname>bean</shortname>
<uri>http://struts.apache.org/tags-bean</uri>
<info>
This tag library contains useful general-purpose tags supporting
the development of JSP/servlet applications based on the "Model
2" (MVC based) design pattern.
WARNING: ALL OF THE TAGS IN THIS LIBRARY ARE DEPRECATED, AND ARE
MAINTAINED ONLY FOR BACKWARDS COMPATIBILITY. THEY WILL BE
REMOVED IN A FUTURE VERSION OF STRUTS.
</info>
<!-- ===================== Template Tags ====================== -->
<!-- Custom tags lib -->
<tag>
<name>table2</name>
<tagclass>lee.Table2Tag</tagclass><!-- 指定业务类 -->
<bodycontent>empty</bodycontent>
<attribute>
<name>table</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
---业务类Table2Tag.java
package lee;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class Table2Tag extends TagSupport {
private String table;
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
@Override
public int doStartTag() throws JspException {
Writer out=pageContext.getOut();
Table2 table2=new Table2(table);//引用类
Iterator it=table2.getIterator();
int num=0;
try {
out.write("<style>td{border:1px solid #000000; padding: 3px;}</style>");
out.write("<table cellspacing='0' cellpadding='0' border='0' align='center' style=\"text-align: center;border: 1px solid #000000;border-collapse: collapse;\">");
out.write("<tr bgColor='9999cc'>");
while(it.hasNext()){
out.write("<td nowrap><b> ");
out.write(it.next().toString());
out.write(" </b></td>");
num++;
}
out.write("</tr>");
String sql="select top 50 * from "+table;
Connection conn = DbHelper.getconn();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
out.write("<tr>");
for (int i = 1; i <= num; i++) {
String clm=rs.getString(i);
clm=clm==null?"":clm;
out.write("<td title=\""+clm+"\" nowrap>");
out.write(clm.length()>25?clm.substring(0, 25)+"...":clm);
out.write("</td>");
}
out.write("</tr>");
}
out.write("</table>");
DbHelper.closeAll();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
}
---引用类Table2.java
package lee;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Table2 implements Serializable {
private Iterator iterator;
private String table;
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public Iterator getIterator() {
return iterator;
}
public void setIterator(Iterator iterator) {
this.iterator = iterator;
}
public Table2() {
super();
}
public Table2(String table) {
super();
this.table = table;
List list=new ArrayList();
try {
String sql="select name from syscolumns Where ID=OBJECT_ID('"+table+"')";
Connection conn=DbHelper.getconn();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
list.add(rs.getObject("name"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbHelper.closeAll();
}
iterator=list.iterator();
}
}
---jsp调用
<%@ taglib prefix="mytag" uri="/WEB-INF/mytaglib.tld" %>
<mytag:table2 table="数据库表名"/>