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 doAfterBody

 
swapna rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
In JSP page i have
<pre:simple>
hello
</pre:simple>

This tag is declared in TLD with body content as JSP and corresponding TagHandler class is given below.

public class body extends TagSupport {
public int doStartTag() throws JspException{
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException {
try { pageContext.getOut().print("how are you?"); }catch(IOException e) {}
return SKIP_BODY;
}
}

what will be the output? Can we write print statements in doAfterBody()?
 
swapna rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please update this thread
 
Vikas Parikh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely ..

One can do any thing that he/she wishes from pageContext.

That is the reason why PageContext is accessible with in the Tag Handler.

You may get different information from PageContext like:

1) ServletRequest: pageContext.getRequest();
2) ServletResponse: pageContext.getResponse();
3) Config: pageContext.getServletConfig();
4) Application: pageContext.getServletContext();
5) Session: pageContext.getSession();
6) Exception: pageContext.getException();
7) Page: pageContext.getPage();
8) Out: pageContext.getOut();

Basically one can access all the 9 implicit objects through Tag Handler

In this case, to the best of my knowledge, output would be

Hello, How are you ?

Vikas Parikh
 
swapna rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen that questions in the following link.
http://www.javaranch.com/carl/SCWCD.htm#

options for the question:
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:
4) The jsp page will print Hello

Explanation is given like below:

you can't print from doAfterBdoy in a TagSupport handlers.You could just repeat the evaluation of the body by returning EVAL_BODY_INCLUDE and stop the loop on some condition by returning SKIP_BODY.

Can anyone explain me what is the right answer for this question and why can't we print statements in doAfterBody()?
 
Vikas Parikh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such restriction imposed at all.

Please execute it your self and check that out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic