Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with TagLib

 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a TagLib which is working perfectly with JSPs,The Tag is

<c:chart id="chart1" datasource="#{name.Graph}" type="xyline" is3d="false" background="green" height="50" width="150" legend="false" border="true"> </c:chart>
. What i am trying to do is set the values of the Taglib Class from within a Servlet Execute it and return back the HTML codes which would otherwise have been returned by the Taglib sitting on a JSP. I have already set the Values



ch_t.setDatasource(tempMap.get("Graph").toString());
ch_t.setId("");
ch_t.setBackground(this.GraphBackground);
ch_t.setLegend(GraphLegend);
ch_t.setBorder(GraphBorder);
ch_t.setIs3d(GraphIs3D);
ch_t.setType(GraphType);
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bad idea. If I understand what you are doing, you are trying to invoke the tag from a servlet reather than a JSP. Since each container will take proprietary steps to perform this action, you're looking at adding container-specific code into your servlet.

Rather, refactor your tag such that it gathers the necessary data from the tag attributes and calls another helper class to format the output. The helper class can also then be directly invoked by a servlet.

This assumes a tag with an empty body. If there is JSP body content that needs to be evaluated, all bets are off.

So why are you trying to do the job of a JSP in a servlet in the first place?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must have been writing this at the same time as Bear, and he just got in first, so I apologise if much of this is the same:

You can't easily do this because the runtime environment of a JSP is slightly different to that of a Servlet - for example, JSPs require PageContext, a JspWriter, scoped attributes, EL parsers and resolvers etc. and it is the JSP engine which manages the lifecycle of a tag and makes sure these objects are available. If your tag was supposed to have body content for example, and you wanted to include scriptlets in it, or you include an EL expression in an attribute (as you have done), what would you (and the container) do?

So you've got a choice: either you can try to hack your specific container to artificially create a proper runtime environment (which is a really bad idea but also a lot of work), or you can redesign your application.

If you really do need to access tag functionality from a servlet (even though a servlet shouldn't really generate textual content as this job is better done by a JSP), perhaps you should put that tag functionality in an independent JavaBean helper, accessed by both the servlet and the tag handler. That way you can do exactly what you need programmatically from both sources.

As I say, if you're really generate a lot of text, you'd be better just using a JSP and using RequestDispatcher to forward to it anyway. You can use a (Http)ServletResponseWrapper when doing the forward if you don't want to put the content of the JSP into the response stream directly.
[ May 08, 2006: Message edited by: Charles Lyons ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic