JSP Custom Tag problem

  • Thread starter Thomas Hoheneder
  • Start date
T

Thomas Hoheneder

Hello,

I have written a class that should calculate the n-th power of a given
number, e. g. number=2, power=3 => result=8.
I work with Eclipse 3.0, Tomcat 5.0.28, JDK 1.4.2, J2EE SDK 1.3.1 and I use
a JSP custom tag to display my calculation result.
My scenario consists of four files:
PowerTag.java (the functionality I use as described above)
powertagdemo.jsp (jsp demo page for my Java functionality)
web.xml (deployment descriptor)
mytld.tld (tag library descriptor)
The full code of these files I have summarized in the file
http://www.thomas-hoheneder.de/temp/java/customtag.txt
for better understanding.
My written class implements javax.servlet.jsp.tagext.IterationTag. There's
also a method named doAfterBody() to be implemented and as far as my book
says this function can is invoked iteratively at run time, depending on the
return value defined. With return value EVAL_BODY_AGAIN doAfterBody() is
invoked one more time and with return value SKIP_BODY it is not invoked any
more. This principle I use to calculate the n-th power of my given number.

The problem now is, that the doAfterBody() NEVER gets invoked which is very
surprising to me. My debug output into the Java console evidences this
theory. My JSP always shows 2^3=1 which means that for the result the
initial value of my result variable is token.
What's wrong here? Why does the doAfterBody() method NEVER get invoked
instead of being invoked iteratively?

Any help to this would be very appreciated. Thanks in advance.

Nice greetings from
Thomas
 
J

John C. Bollinger

Thomas said:
Hello,

I have written a class that should calculate the n-th power of a given
number, e. g. number=2, power=3 => result=8.
I work with Eclipse 3.0, Tomcat 5.0.28, JDK 1.4.2, J2EE SDK 1.3.1 and I use
a JSP custom tag to display my calculation result.
[...]

My written class implements javax.servlet.jsp.tagext.IterationTag. There's
also a method named doAfterBody() to be implemented and as far as my book
says this function can is invoked iteratively at run time, depending on the
return value defined. With return value EVAL_BODY_AGAIN doAfterBody() is
invoked one more time and with return value SKIP_BODY it is not invoked any
more. This principle I use to calculate the n-th power of my given number.

That's a misuse of the iteration mechanism, IMO. There is no special
reason why you would need to perform the calculation that way, and in
fact it's more complicated to do so than to implement only
javax.servlet.jsp.tagext.Tag and do the computation entirely in
doStartTag() (or in doEndTag()).
The problem now is, that the doAfterBody() NEVER gets invoked which is very
surprising to me. My debug output into the Java console evidences this
theory. My JSP always shows 2^3=1 which means that for the result the
initial value of my result variable is token.
What's wrong here? Why does the doAfterBody() method NEVER get invoked
instead of being invoked iteratively?

In your test JSP, the custom tag has an empty body. The JSP tag
iteration mechanism is skipped in such cases. This is part of the spec,
although it isn't very prominent there. Your code would be much simpler
if your tag handler subclassed TagSupport and overrode doStartTag() to
perform and output the computation. You would need to implement
setNumber(int) and setPower(int). Your own code would only need those
three methods, plus instance variables to hold the two attribute values.
Simpler, cleaner, and works.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top