一,tag类
1.1 TagMy标签类,格式化当前日期并输出
package com.dkt.tag;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class TagMy extends TagSupport{
private String format;
public int doStartTag() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
String nowdate = simpleDateFormat.format(new Date());
JspWriter out = pageContext.getOut();
try {
out.print("Hello 世界!!");
out.print(nowdate);
} catch (IOException e) {
e.printStackTrace();
}
return super.SKIP_BODY;
}
public int doAfterBody() throws JspException {
return super.doAfterBody();
}
public int doEndTag() throws JspException {
return super.doEndTag();
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
}
1.2 ForTag 标签类,自定义标签,循环输出
package com.dkt.tag;
import javax.servlet.jsp.tagext.TagSupport;
public class FroTag extends TagSupport{
private int start;
private int end;
private String var;
public int doStartTag(){
System.out.println("doStartTag()"+start);
if(start<=end){
pageContext.setAttribute(var, start);
start++;
return super.EVAL_BODY_INCLUDE;
}else {
return super.SKIP_BODY;
}
//执行doAfterBody()
}
public int doAfterBody() {
if(start<=end){
pageContext.setAttribute(var, start);
start++;
System.out.println("doAfterBody()"+start);
return super.EVAL_BODY_AGAIN;
}else {
return super.SKIP_BODY;
}
}
public int doEndTag(){
System.out.println("doEndTag()"+start);
return super.EVAL_PAGE;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
}
1.3 IteratorTag标签类,迭代集合,全部循环输出显示在页面
package com.dkt.tag;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class IteratorTag extends TagSupport{
private String scope;
private String var;
private ArrayList list = null;
Iterator it = null;
public int doStartTag() {
it = list.iterator();
return super.EVAL_BODY_INCLUDE;
}
public int doAfterBody() {
if (it.hasNext()) {
Object next = it.next();
pageContext.setAttribute(var, next);
return super.EVAL_BODY_AGAIN;
}else {
return super.SKIP_BODY;
}
}
public int doEndTag() throws JspException {
return super.doEndTag();
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public ArrayList getList() {
return list;
}
public void setList(ArrayList list) {
this.list = list;
}
}
1.4 QueryTag 标签类,根据表名,查询数据库,并由表格输出到页面
package com.dkt.tag;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.dkt.util.DButil;
public class QueryTag extends TagSupport{
private String tableName;
public int doStartTag() {
DButil dButil = new DButil();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
conn = dButil.createConn();
String sql = "select * from "+tableName;
try {
ps = conn.prepareStatement(sql);
ResultSetMetaData rsmd = ps.getMetaData();//得到表结构
rs = ps.executeQuery();
JspWriter out = pageContext.getOut();
out.print("
for (int i = 0; i < rsmd.getColumnCount(); i++) {//字段数量
out.print("
"+rsmd.getColumnName(i+1)+"");//字段名字
}
out.print("
");
while(rs.next()){
out.print("
");
for (int i = 0; i < rsmd.getColumnCount(); i++) {
out.print("
"+rs.getObject(i+1)+"");
}
out.print("
");
}
out.print("
");
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return super.SKIP_BODY;
}
public int doAfterBody() throws JspException {
return super.doAfterBody();
}
public int doEndTag() throws JspException {
return super.doEndTag();
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}
二,hellotag.tld
文件 hellotag.tld 放在WEB-INF目录下,与web.xml同级
/p>
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
1.0
1.2
hello
hello
hello
com.dkt.tag.TagMy
empty
format
true
true
formy
com.dkt.tag.FroTag
jsp
start
true
true
end
true
true
var
true
true
iteratormy
com.dkt.tag.IteratorTag
jsp
list
true
true
scope
true
true
var
true
true
query
com.dkt.tag.QueryTag
empty
tableName
true
true
三,jsp
index.jsp 开启服务器,直接访问项目下的index.jsp页面即可显示自定义标签
在jsp页面导入自定义标签
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
formy:
${item1 }----
iterator:
ArrayList list = new ArrayList();
list.add(new UserInfo(0,"小白","123456","",""));
list.add(new UserInfo(1,"小白111","353456","",""));
list.add(new UserInfo(2,"小白222","532456","",""));
list.add(new UserInfo(3,"小白333","532356","",""));
list.add(new UserInfo(4,"小白444","123536","",""));
request.setAttribute("list",list);
%>
${item2.name}+++${item2.password }
四,web.xml
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
index.jsp