Consider the following
JSP page code and SimpleTag code. What is the output from tag <mytags:question08> to the requesting JSP page? You can assume that all necessary deployment descriptor and tag library descriptor elements are set up correctly. (Choose one.)
JSP Page Code:
<%@ taglib prefix="mytags" uri="http://www.osborne.com/taglibs/mytags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><head><title>Question 10</title></head>
<c:set var="counter">1</c:set>
<body><mytags:question10>
<c:forEach begin="${counter}" end="3">${counter}</c:forEach>
</mytags:question10></body>
</html>
SimpleTag Tag Handler Code for <mytags:question08>:
package webcert.ch09.questions09;
import java.io.IOException;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class Question10 extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
JspContext context = getJspContext();
int i = Integer.parseInt("" + context.getAttribute("counter"));
for (; i < 4; i++) {
context.setAttribute("counter", new Integer(i));
getJspBody().invoke(null);
}
}
}
A.No output
B.111 222 333
C.1 2 3
D.123 123 123
E.111 22 3
F.22 3
The answer is E.
But, I run the code, it won't work.
My question is: Can tagl hander get counter attribute when the counter defined in the jsp file by <c:set var="counter">1</c:set>?
I make it work by adding <%pageContext.setAttribute("counter","1" );%> in the jsp file, the output is 123
Any explaination, please?
Your help is much apprepricated.
Thanks
Tiffiny