自定义类:importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.jsp.JspException;importjavax.servlet.jsp.tagext.TagSupport;public class CalcTag extendsTagSupport {private intnumA;private intnumB;public intgetNumA() {returnnumA;
}public void setNumA(intnumA) {this.numA =numA;
}public intgetNumB() {returnnumB;
}public void setNumB(intnumB) {this.numB =numB;
}
@Overridepublic int doStartTag() throwsJspException {
ServletResponse sr= super.pageContext.getResponse();try{
PrintWriter out=sr.getWriter();
out.println("
");
out.print(numA);
out.print("+");
out.print(numB);
out.print("=");
out.println(numA+numB);
out.println("");
out.flush();
out.close();
}catch(IOException e) {
e.printStackTrace();
}returnSKIP_BODY;
}
}
创建后缀为tld的文件:<?xml version="1.0" encoding="UTF-8"?>
1.0
2.0
Example TLD with Body
calc
com.wisezone.tag.CalcTag
empty
numA
true
true
numB//变量名首字母不能大写
true
true
web.xml中配置注册:<?xml version="1.0" encoding="UTF-8"?>
tagweb
index.html
/wisezone
/WEB-INF/mytag.tld
jsp页面使用:
Insert title here