aspose file tools*
The moose likes Servlets and the fly likes Problem with TagLib Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with TagLib" Watch "Problem with TagLib" New topic
Author

Problem with TagLib

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
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

Joined: Jan 10, 2002
Posts: 61656
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with TagLib