This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
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

custom tag -> include file -> custom tag

 
Harpartap Singh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a sanity check. I am implementing templates using custom tags. The custom tag calls pageContext.include() which in turn intantiates the same custom tag.Here is the scenario, question, and code
I realize the fact that "if a custom tag occurs multiple times on a JSP page, a single instance of the tag handler may be used to handle all of those occurences"
In this case, fileA.jsp instantiates the RenderTag (template:render). The nested tag PutTag (template ut) gets the section/content pairs of values and stores the values in the REQUEST_SCOPE of the pageContext useing pageContext.setAttribute(section,content,PageContext.REQUEST_SCOPE) . The PutTag doesn't play any more part.
Next the doEndTag() of RenderTag instance dynamically includes the file "templateFile.jsp".
This is where I have questions:
1. In the included file the RenderTag is instantiated again. It instantiates a NEW RenderTag object. CORRECT?
2. But since this included file is a NEW jsp file, it has its own NEW pageContext , but calling pageContext.getAttribute(section,REQUEST_SCOPE), should retrieve the ORIGINAL section/content pair that I had stored in the outside files pageContext. I believe this is the case, because the included file did get the SAME request object, and the stored section/content pair of values came with it. CORRECT?
I have tested this and it works. I want to confirm that my understanding of same tag instantiation in included files is correct. And I also want to confirm my understanding that sharing attributes using REQUEST_SCOPE of different pageContexts is a safe way, as long as we are dealing with "included" files in the same request.
realFile.jsp

templateFile.jsp

RenderTag.java

Thank you so much for reading this far.
/hs
 
Shawn Bayern
Author
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, your understanding is correct. Specifically:
1. In the included file the RenderTag is instantiated again. It instantiates a NEW RenderTag object. CORRECT?

Yes. Note that this has very little to do with whether there are inclusions, forwards, or anything else. The key concept is that while the JSP specification permits a container to reuse instances of tag-handler implementation classes, the same instance can never be reused while it's currently being called by an invocation. If you had
<a:b>
  <a:b/>
</a:b>
in a single page, this would necessitate two different instances of the handler for <a:b>. The situation you're describing is just a more elaborate version of this same situation, as far as tag-handler lifecycles are concerned.
2. But since this included file is a NEW jsp file, it has its own NEW pageContext

Yes, specifically true.
but calling pageContext.getAttribute(section,REQUEST_SCOPE), should retrieve the ORIGINAL section/content pair that I had stored in the outside files pageContext. I believe this is the case, because the included file did get the SAME request object, and the stored section/content pair of values came with it. CORRECT?

Exactly. "Request scope" travels with (and in fact is identical with and implemented in terms of) the ServletRequest object. Request scope is specifically intended for situations where you want data to traverse multiple pages that are all used to respond to a single web request: that is, inclusions and forwards.
Hope that helped, and thanks for the interesting questions!
[ September 24, 2002: Message edited by: Shawn Bayern ]
 
Harpartap Singh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shawn, thanks a lot for answering my verbose questionaire. (it was actually a code review in disguise )
I guess I should get the "JSTL in Action" and start reading it.
thanks,
/hs
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic