范例一:使用自定义Tag去读数据库中的信息
TagDB.java代码如下
package tag.test;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
import java.sql.*;
public class TagDB extends TagSupport
{
Connection connection=null;
Statement statement=null;
ResultSet rset=null;
public TagDB()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
public int doStartTag()throws JspTagException
{
return EVAL_BODY_INCLUDE;
}
public int doEndTag()throws JspTagException
{
try
{
JspWriter out=pageContext.getOut();
connection=DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=pubs;user=sa;password=1234");
statement=connection.createStatement();
rset=statement.executeQuery("select * from authors");
out.print("以下使用Tag输出作者的基本信息");
out.print("
"); out.print("
");
}
catch(Exception e)
{
e.printStackTrace();
}
return EVAL_PAGE;
}
}
author.tld描述文件如下
<?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.2</tlib-version><jsp-version>1.2</jsp-version><short-name>DB</short-name><description>
this taglib provides tag to show date
</description><tag><name>DBShow</name><tag-class>tag.test.TagDB</tag-class><body-content>empty</body-content></tag></taglib>
web.xml文件内容如下
<?xml version="1.0" encoding="ISO-8859-1"?><web-app><display-name>welcome to Tomcat</display-name><description>
Welcome to Tomcat
</description><taglib><taglib-uri>DB</taglib-uri><taglib-location>/WEB-INF/author.tld</taglib-location></taglib></web-app>
tagTest.jsp文件如下
<dbshow></dbshow>
范例二:使用Tag标签向数据库中查入数据,插入成功后将所有记录显示出来.
InfoTagDB.java 代码如下
package tag.test;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.sql.*;
public class InfoTagDB extends TagSupport
{
String strNumber;
String strName;
int strNum;
Connection connection=null;
Statement statement=null;
public InfoTagDB()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
public void setStrNumber(String strNumber)
{
this.strNumber=strNumber;
}
public void setStrName(String strName)
{
this.strName=strName;
}
public void setStrNum(int strNum)
{
this.strNum=strNum;
}
public int doStartTag()throws JspTagException
{
JspWriter out=pageContext.getOut();
try
{
connection=DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=pubs;user=sa;password=1234");
statement=connection.createStatement();
String sql="insert into authors(au_id,au_lname,au_fname,phone,address,city) values('"+au_id+"','"+au_lname+"','"+au_fname+"','"+phone+"','"+address+"','"+city + "')";
int i=statement.executeUpdate(sql);
if(i>0)
{
out.print("插入成功");
}
else
{
out.print("插入失败");
}
statement.close();
connection.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return SKIP_BODY;
}
}
insert.tld标签描述文件如下
<?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.2</tlib-version><jsp-version>1.2</jsp-version><shor-name>mytag</shor-name><description>the info test example</description><tag><name>insert</name><tag-class>tag.test.InfoTagDB</tag-class><body-content>empty</body-content><attribute><name>au_id</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>au_lname</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>au_fname</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>phone</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>address</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>city</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag></taglib>
web.xml配置文件如下
<?xml version="1.0" encoding="ISO-8859-1"?><web-app><display-name>welcome to Tomcat</display-name><description>
Welcome to Tomcat
</description><taglib><taglib-uri>DBInsert</taglib-uri><taglib-location>/WEB-INF/insert.tld</taglib-location></taglib><taglib><taglib-uri>DB</taglib-uri><taglib-location>/WEB-INF/author.tld</taglib-location></taglib></web-app>
tagTest.jsp文件如下
<insert au_id="888_666" au_lname="jane" au_fname="peng" phone="15853208264" address="中国海洋大学" city="青岛"></insert><dbshow></dbshow>
对于tagTest.jsp文件中的第二个标签是范例一TagDB.java中的代码,大家可以参考。
范例三:使用Tag删除数据库中的记录,删除后将所有数据库中的内容显示出来
TagDelete.java代码如下
package tag.test;
import java.sql.*;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class TagDelete extends TagSupport
{
String au_id;
Connection connection=null;
Statement statement=null;
public TagDelete()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
public void setAu_id(String s)
{
this.au_id=s;
}
public int doStartTag()throws JspTagException
{
try
{
JspWriter out=pageContext.getOut();
connection=DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=pubs;user=sa;password=1234");
statement=connection.createStatement();
String sql="delete * from authors where au_id='"+au_id+"'";
int i=statement.executeUpdate(sql);
if(i>0)
{
out.print("删除成功
"); } else { out.print("删除失败
"); } statement.close(); connection.close(); } catch(Exception e) { e.printStackTrace(); } return SKIP_BODY; } public int doEndTag()throws JspException { return EVAL_PAGE; } } delete.tld标签描述文件如下 <?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.2</tlib-version><jsp-version>1.2</jsp-version><shor-name>Deltag</shor-name><description>the info test example</description><tag><name>delete</name><tag-class>tag.test.TagDelete</tag-class><body-content>empty</body-content><attribute><name>au_id</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag></taglib> web.xml配置文件如下 <?xml version="1.0" encoding="ISO-8859-1"?><web-app><display-name>welcome to Tomcat</display-name><description> Welcome to Tomcat </description><taglib><taglib-uri>DBInsert</taglib-uri><taglib-location>/WEB-INF/insert.tld</taglib-location></taglib><taglib><taglib-uri>DB</taglib-uri><taglib-location>/WEB-INF/author.tld</taglib-location></taglib><taglib><taglib-uri>Deltag</taglib-uri><taglib-location>/WEB-INF/delete.tld</taglib-location></taglib></web-app> tagTest.jsp测试代码如下 <delete au_id="172-32-8886"></delete><dbshow></dbshow> 对于tagTest.jsp文件中的第二个标签是范例一TagDB.java中的代码,大家可以参考 范例五:使用自定义标签实现字符串的大小写转化。 ConvertTest.java代码如下 package tag.test; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class ConvertTest extends BodyTagSupport { public int doAfterBody()throws JspException { try { BodyContent body=getBodyContent(); JspWriter out=body.getEnclosingWriter(); String text=body.getString();//获取标签中的内容 if(text!=null) { out.print(text.toLowerCase()); } } catch(Exception e) { e.printStackTrace(); } return SKIP_BODY; } } convert.tld标签描述文件如下 <?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.2</tlib-version><jsp-version>1.2</jsp-version><shor-name>mytag</shor-name><description>the info test example</description><tag><name>Convert</name><tag-class>tag.test.ConvertTest</tag-class><body-content>jsp</body-content></tag></taglib> web.xml配置文件如下 <?xml version="1.0" encoding="ISO-8859-1"?><web-app><display-name>welcome to Tomcat</display-name><description> Welcome to Tomcat </description><taglib><taglib-uri>mytag</taglib-uri><taglib-location>/WEB-INF/convert.tld</taglib-location></taglib></web-app> testTag.jsp测试文件如下 <convert> Hello FRIEND! THIS A PROGRAMM TO CONVERT BIG CHAR TO SMALL CHAR; </convert>
"); out.print("
au_id | au_lname | au_fname | phone | address | city |
"+rset.getString(1)+" | "+rset.getString(2)+" | "+rset.getString(3)+" | "+rset.getString(4)+" | "+rset.getString(5)+" | "+rset.getString(6)+" |
"); } else { out.print("删除失败
"); } statement.close(); connection.close(); } catch(Exception e) { e.printStackTrace(); } return SKIP_BODY; } public int doEndTag()throws JspException { return EVAL_PAGE; } } delete.tld标签描述文件如下 <?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.2</tlib-version><jsp-version>1.2</jsp-version><shor-name>Deltag</shor-name><description>the info test example</description><tag><name>delete</name><tag-class>tag.test.TagDelete</tag-class><body-content>empty</body-content><attribute><name>au_id</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag></taglib> web.xml配置文件如下 <?xml version="1.0" encoding="ISO-8859-1"?><web-app><display-name>welcome to Tomcat</display-name><description> Welcome to Tomcat </description><taglib><taglib-uri>DBInsert</taglib-uri><taglib-location>/WEB-INF/insert.tld</taglib-location></taglib><taglib><taglib-uri>DB</taglib-uri><taglib-location>/WEB-INF/author.tld</taglib-location></taglib><taglib><taglib-uri>Deltag</taglib-uri><taglib-location>/WEB-INF/delete.tld</taglib-location></taglib></web-app> tagTest.jsp测试代码如下 <delete au_id="172-32-8886"></delete><dbshow></dbshow> 对于tagTest.jsp文件中的第二个标签是范例一TagDB.java中的代码,大家可以参考 范例五:使用自定义标签实现字符串的大小写转化。 ConvertTest.java代码如下 package tag.test; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class ConvertTest extends BodyTagSupport { public int doAfterBody()throws JspException { try { BodyContent body=getBodyContent(); JspWriter out=body.getEnclosingWriter(); String text=body.getString();//获取标签中的内容 if(text!=null) { out.print(text.toLowerCase()); } } catch(Exception e) { e.printStackTrace(); } return SKIP_BODY; } } convert.tld标签描述文件如下 <?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.2</tlib-version><jsp-version>1.2</jsp-version><shor-name>mytag</shor-name><description>the info test example</description><tag><name>Convert</name><tag-class>tag.test.ConvertTest</tag-class><body-content>jsp</body-content></tag></taglib> web.xml配置文件如下 <?xml version="1.0" encoding="ISO-8859-1"?><web-app><display-name>welcome to Tomcat</display-name><description> Welcome to Tomcat </description><taglib><taglib-uri>mytag</taglib-uri><taglib-location>/WEB-INF/convert.tld</taglib-location></taglib></web-app> testTag.jsp测试文件如下 <convert> Hello FRIEND! THIS A PROGRAMM TO CONVERT BIG CHAR TO SMALL CHAR; </convert>
发表评论
-
对jsp、servlet的总结
2010-10-05 11:35 1134一.Jsp与servlet的区别: ... -
自定义标签
2010-10-03 22:41 848为什么要使用自定义标签呢?主要是为了取代丑陋的JSP脚本。在H ... -
DWR的简单介绍
2010-10-03 22:34 1050Dwr的简单介绍: DWR 是一个可以允许你去创建AJAX ... -
DWR
2010-10-03 22:32 1388****************************Dwr ... -
Ajax的优缺点
2010-10-03 22:30 1007Ajax优缺点 AJAX的优点:AJAX是指一种创建交互式网 ... -
Ajax的执行流程
2010-10-03 22:29 1629Ajax的执行流程: 1.发出一个Http请求:通过XMLH ... -
Ajax笔记
2010-10-03 22:28 966Ajax 1. Ajax(Asynchronou ... -
JSP
2010-09-18 18:06 1785...
相关推荐
ztree自定义tag
自定义tag学习,封装ext代码,还待整理
开源PHP论坛系统 所有...安装过程中可选择中文简体语言,编码是UTF-8,它具备基于角色的权限控制系统、短消息发送平台、审查与帖子回收系统;支持附件/IP跟踪/投票/电子报/全文搜索/用户禁用/自定义Tag/用户等级等功能
博文链接:https://buaalijie.iteye.com/blog/241145
主要介绍了asp.net core razor自定义taghelper的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
NULL 博文链接:https://mr-cheney.iteye.com/blog/1057176
· 什么是自定义tag标签? · 怎么使用tag标签? o 声明要使用的tag库 o 找到与之对应的tag处理类 o tag标签的类型 · 自定义tag标签 o tag处理类 o tag库描述 o tag标签示例 o 带属性的tag o ...
GIT_Tag 一个Java自定义TAG库,用于在网页上显示GIT构建和提交信息。
自定义标签实例 myTag
老师给例子与大家分享
jsp自定义的标签如何打包成jar包,让你轻松导入项目中,本资源让你一目了然
该项目可以直接放入myelipse中直接运行。
自定义多选下拉框标签,只需引用标签即可实现多选下拉框,可以传入参数来改变样式等
因为在JSP页面下混合HTML代码和JAVA代码,导致JSP页面变得难维护,可重用性低,使用JSP标签元素后,JSP页面变得简洁和趋向XML格式语法
源码HZQTagCollectionView,TagCollectionView(标签按钮、随意添加个数、自动布局) *使用UICollectionView *可自定义高度和每行的个数 *可自定义颜色或样式 *高手轻喷。 // 每行个数 #define RowCount 4 // 每个...
tag struts2 自定义标签 实例 tag struts2的自定义标签实例
jQuery自定义标签输入插件Tagator是一款jQuery自定义tag标签输入插件Tagator,在输入框内输入标签后按回车键即可键入tag,输入的标签也可以删掉,内置自动补全效果,支持过滤重复标签。
实现自定义标签功能,使用maven引入库,详细使用请查看我的博文 https://blog.csdn.net/TT18949344290/article/details/79836691
原始基因标签(PGGT) PGGT是一个protoc插件,用于在生成的protobuf消息上添加/替换struct标签。...添加/替换标签 可以将新标签(如xml,sql,bson等)添加到protobuf的结构消息中。 例子 syntax = "proto3" ;...