0

I'm writing a custom taglib (1.2) which contains loop tag and nested ones. Does anyone know if there's a way, inside a first loop tag, to know if there is more occurrences of the same loop tag later in the JSP ?

Example :

<tt:myLoopTag>
    <%--  
       Normally, if "myLoopTag" was the only one tag in the JSP, 
       I would do some stuff in the doEndTag method
    --%>
</tt:myLoopTag>

...
 
<tt:myLoopTag>
    <%--
       But I have a second "myLoopTag" here, so I would like to do
       some stuff in the doEndTag method of this tag, not in the first one
    --%>
</tt:myLoopTag>

Hope I'm clear enough ..

NB: Unfortunately, I don't think this post is useful to me ...

Thanks !

Community
  • 1
  • 1
tduchateau
  • 4,351
  • 2
  • 29
  • 40

1 Answers1

0

I think this is very unlikely to be possible. If you look the Java code generated for a JSP page (for example, in tomcat/work) you will see why.

JSP pages are compiled into Servlets, which write to the JspWriter stream sequentially. For what you are trying to do to be possible the Java code would have to either be able to 'look ahead' at code that will be executed later in the Servlet, or it would need to retrospectively change data that has already been written to the JspWriter stream. It may be technically possible to do one or the other of these but if it is I suspect it would involve much hackery.

It is worth considering why you need to do this. How about posting details of your actual objective to see if anyone can suggest an alternative approach?

Martin Wilson
  • 3,386
  • 1
  • 24
  • 29
  • Thanks Martin. Indeed, I think I should give some details about the actual objective. In the doEndTag method of myLoopTag, there are several steps : – tduchateau Sep 21 '12 at 14:00
  • 1) all the nested tags are collected and computed to product some web assets (Javascript files and CSS files, as strings) which are stored in a ServletContext attribute – tduchateau Sep 21 '12 at 14:01
  • 2) HTML script and link tags are generated, and all call a custom servlet which returns, depending on the file name, javascript content or css content. And because it's not difficult enough, among javascript assets, some could be shared accross the multiples myLoopTag and some are "tag-specific". – tduchateau Sep 21 '12 at 14:01
  • I was thinking : what about using Servlet filter ? If I'm not mistaken, after the chain.doFilter() call (inside doFilter()), in theory, all the JSPs should be interpreted. So I need to find a way to populate something (filter class attribute? servletcontext attribute?) during the JSP processing, use it after the chain.doFilter() method and remove it before the end of the doFilter method. Could it work ? :-) – tduchateau Sep 21 '12 at 14:09