MyTag.java
package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport
{
private int counter = 0;
private String[] names = null;
public int doStartTag()
{
names = (String[]) pageContext.getAttribute("names");
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException
{
try{
pageContext.getOut().print(" " + names[counter] + "
");
} catch(Exception e){
throw new JspException(e.toString());
}
counter++;
if(counter >= names.length) {
return SKIP_BODY;
}
return EVAL_BODY_AGAIN;
}
}
MyTag.tld
"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
1.0
1.2
TagExamples
Example tags.
iterator
taglib.MyTag
web.xml
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
Example web application illustrating the use of tags in the
"request" custom tag library, from the JAKARTA-TAGLIBS project.
http://rntsoft.com/taglibs
/WEB-INF/MyTag.tld
admin
Jsp page with custom tag
<%@ taglib prefix="l" uri="WEB-INF/MyTag.tld" %>
Supporting Iterating Custom Tags
Supporting Iterating Custom Tags
<%
String[] names = new String[]{ "A", "B", "C", "D" };
pageContext.setAttribute("names", names);
%>
Cast member: