Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in question 10 of chapter 9 in SCWCD Study Guide - by David Bridgewater

 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is supposed to be answer E. In the Simple Tag, you can get the page context (and all attributes in the page scope) via getJspContext().
Are you using JSTL1.1 ? Which implementation ?
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replay.

I am using JSTL1.1, and Tomacat 5.5.
I get "java.lang.NumberFormatException: For input string: "null" due to counter is null.

What could be wrong on my side?

Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I can't see anything wrong here.
It is the same, but can you try instead of

Can you also show your TLD ? Is "question10" associated to "webcert.ch09.questions09.Question10.class" ?
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satou again.

I have to replace
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
with
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
then I will be able to use <c:set var="counter" value="1" /> in the code with result displayed 111223.

I am using Tomcat 5.5.17, and JSTL 1.1, I wonder why can't I use <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>.

Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange. Did you follow the following instructions ? :
http://faq.javaranch.com/view?JstlTagLibDefinitions

Where did you get JSTL's JARs from ?
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the standard.jar and jstl.jar from Tomcat.

\\apache-tomcat-5.5.17\webapps\jsp-examples\WEB-INF\lib

Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know which version is in it. Try with Apache's implementation :
http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic