jdbctag.com:
<taglib> <shortname>table</shortname> <tag> <name>show</name> <tagclass>tagpack.JdbcTag</tagclass> <bodycontent>empty</bodycontent> <attribute> <name>username</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>password</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>dsn</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>table</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>
注意:我们应该按照我们使用的JSP程序的顺序编写相同的属性。
hellotag.java(标记处理程序类):
package t1; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class HelloTag extends TagSupport { private String name; public void setName(String name) { this.name = name; } public int doEndTag() throws JspException { try { pageContext.getOut().print("Hello..! " + name); } catch (Exception e) { throw new JspException(e.getMessage()); } return EVAL_PAGE; } };
jdbctag.jsp:
<html> <body> <%@ taglib prefix="table" uri="/WEB-INF/tlds/JdbcTag.com" %> <center> <table:show username="scott" password="tiger" dsn="oradsn" table="product"> </center> </body> </html>
TagSupport类示例- 使用自定义标记在浏览器上打印键入的名称
hellotag.jsp:
<%@ taglib uri="/WEB-INF/tlds/hello.com" prefix="test" %> <html> <h3>This is example on custom tag</h3> <h4><test:hello name="Hyderabad"</h4> </html>
jdbctag.java:
package tagpack; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.sql.*; import java.io.*; public class JdbcTag extends TagSupport { String table, username, password, dsn; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setDsn(String dsn) { this.dsn = dsn; } public void setTable(String table) { this.table = table; } Connection con = null; PreparedStatement ps = null; ResultSet rs = null; ResultSetMetaData rsmd = null; public int doStartTag() throws JspException { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:" + dsn, username, password); ps = con.prepareStatement("select * from " + table); return EVAL_BODY_INCLUDE; } catch (Exception e) { throw new JspException(e.getMessage()); } } public int doEndTag() throws JspException { int i = 1; try { rs = ps.executeQuery(); rsmd = rs.getMetaData(); pageContext.getOut().print("<table border=1><tr>"); for (i = 1; i <= rsmd.getColumnCount(); i++) { pageContext.getOut().print("<th>" + rsmd.getColumnName(i) + "</th>"); } pageContext.getOut().print("</tr>"); while (rs.next()) { pageContext.getOut().print("<tr>"); for (i = 1; i <= rsmd.getColumnCount(); i++) { pageContext.getOut().print("<td>" + rs.getString(i) + "</td>"); } pageContext.getOut().print("</tr>"); } pageContext.getOut().print("</table>"); return EVAL_PAGE; } catch (Exception e) { throw new JspException(e.getMessage()); } } };
JSP开发标签处理程序类:
标签处理程序类基本上是javabeans类,它应该包含集合方法和Get方法集。
- 自定义标记的所有属性必须用标记处理程序类中的属性或者数据成员以相同的顺序。
- 每个标签处理程序类必须扩展名为javax.servlet.jsp.tagext.tagsupport的预定义类
- tagsupport类包含以下两个生命周期方法(JSP容器自动调用),它们必须在标记处理程序类中覆盖。
DostartTag返回的有效值是:
jsp容器将在截止自定义标记时调用dostarttag()方法<x:hello 。
Skip_Body是DostartTag()方法返回的常量,当我们不想执行自定义标记的正文时。
eval_body_include是DostartTag()方法返回的常量,当我们想要评估自定义标记的正文时。
DoisdTag()返回的有效值是:
JSP容器将调用DoendTAG()方法当截取的自定义标签的结束标签<x:hello 时。
eval_page是DoisdTag()方法返回的常量,当我们想要执行JSP页面的其余部分时。
Skip_page是DoisdTag()方法返回的常量,当我们不想执行JSP页面的其余部分时。
JSP标签处理程序类 示例-使用自定义标签与数据库连接
hello.com:
<taglib> <shortname>test</shortname> <tag> <name>hello</name> <tagclass>t1.HelloTag</tagclass> <bodycontent>empty</bodycontent> <attribute> <name>name</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>
注意:pagecontext是javax.servlet.jsp.pageContext界面的对象,它将由JSP容器自动创建。
PageContext对象是在Tagsupport类中预先声明的,并且此对象将可供TagSupport类的每个和每个子类使用。
web.xml:
<web-app> </web-app>
web.xml:
<web-app> </web-app>
日期:2020-04-11 23:04:38 来源:oir作者:oir