• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused with jsp result

 
tony lee
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need a help. The following is a question:

what will be printed out by the following part of a jsp page?
<prefix:sufix>
<i>Hello</i>
</prefix:sufix>
1) The tag handler won't compile.
2) The jsp page will print Hello how are you?
3) The jsp page will print how are you? Hello
4) The jsp page will print Hello
Answer is 4.
Could someone try it on your machine? I tried and found the right answer should be 1

avax.servlet.ServletException: Since tag handler class util.body does not implement BodyTag, it can't return BodyTag.EVAL_BODY_TAG ...

Thanks,
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what vrersion of tomcat are u using
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class body extends BodyTagSupport
[ snip ]
avax.servlet.ServletException: Since tag handler class util.body does not implement BodyTag
[ snip ]

How can that be possible?
- madhav
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, TC version will matter very much, there have been a number of bugs in this area.
Check out the TC user and dev archives:
here
Regards, Guy
 
Gaurav Chikara
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same question was asked sometime back
but correct output was
How are you?Hello
anyone of sheriifs like Satya/Peter den haan can clarify pls
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Same question was asked sometime back
but correct output was
How are you?Hello
anyone of sheriifs like Satya/Peter den haan can clarify pls


Here is my research into the subject.......

  • Case 1: Using code given in the original post....
    In this case doStartTag(...) returns EVAL_BODY_BUFFERED by default. Hence, the body is evaluated and added to the new buffer that is created, not to the output Stream. Then the doAfterBody(...) comes along and says, never mind SKIP_BODY. Hence the body is skipped completely. ie; neither "Hello" nor "How are you?" is returned to the client.
  • Case 2: Using code given in the original post and now overriding the doStartTag(...) to return EVAL_BODY_INCLUDE....
    In this case doStartTag(...) returns EVAL_BODY_INCLUDE. Hence, the Body is evaluated and included in the output stream (not a buffer). Then the doAfterBody(...) comes along and says, never mind SKIP_BODY. Hence the body is skipped henceforth. But, notice that the Body was already evaluated and added to the output Stream once, in the doStartTag(...). So the client will see an output "HelloHow are you?".
  • Case 3: Another slight variation but same result as case two above. Interesting to note.
    In this case I extend TagSupport instead of BodyTagSupport class.

  • doStartTag(...) returns EVAL_BODY_INCLUDE. Hence, the Body is evaluated and included in the output stream (not a buffer, which would be illegal resulting in a compile time error). There is no doAfterBody(...) when extending TagSupport class.
    Since the Body was already evaluated and added to the output Stream once, in the doStartTag(...). So the client will see an output "HelloHow are you?".

    Hope this helps.
    regds.
    - madhav
    [ April 15, 2002: Message edited by: Madhav Lakkapragada ]
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    /*******************
    Case 3: Another slight variation but same result as case two above. Interesting to note.
    In this case I extend TagSupport instead of BodyTagSupport class.
    doStartTag(...) returns EVAL_BODY_INCLUDE. Hence, the Body is evaluated and included in the output stream (not a buffer, which would be illegal resulting in a compile time error). There is no doAfterBody(...) when extending TagSupport class.
    Since the Body was already evaluated and added to the output Stream once, in the doStartTag(...). So the client will see an output "HelloHow are you?".
    ******************/
    public class body extends TagSupport {
    public int doStartBody() throws JspException
    {
    return EVAL_BODY_INCLUDE;
    }
    public int doAfterBody() throws JspException
    {
    try {
    pageContext.getOut().print("how are you?");
    }catch(IOException e) {}
    return SKIP_BODY;
    }
    }
    }
    should'nt it print just "Hello"
    as doAfterBody will never be called
     
    Madhav Lakkapragada
    Ranch Hand
    Posts: 5040
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    should'nt it print just "Hello"
    as doAfterBody will never be called

    Yes, it does. My mistake.
    Thanks for verifying, feels good that someone reads my posts....
    regds.
    - madhav
     
    George Pavamani
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Satya,
    I was just looking through your message. Case1 and case2 make perfect sense to me but not case 3.
    You said there is no doAfterBody() in TagSupport class which is not true because TagSupport implements the IterationTag interface.
    Correct me if i am wrong.
    Thanks
    George
     
    Madhav Lakkapragada
    Ranch Hand
    Posts: 5040
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Case 3 is invalid, please read the discussion immediately above your posting.
    Maybe I should edit my original post itself.....
    Thanks.
    - madhav
     
    George Pavamani
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Satya,
    Sorry i read it wrong. I thought (in case 3) you are extending the TagSupport and overriding the doStartTag() method to return EVALUTE_BODY_INCLUDE, and thats why all this confusion.
    Thanks for reply
    George
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic