Image

Imagespecialagentm wrote in Imagejava_dev 😯confused

Listens: Infected Mushroom - "Release Me"

Sending non-HTML data from a JSP file?

OK, this one has me whipped. I know I'm probably missing something simple here. Frustrates me that after 4 years of doing this, I can't figure out how to fix this problem.

My basic problem is, I want to send non-HTML content out via a JSP page. But let me go into more detail, just to show I'm not completely stupid :-).



I have a web application that uses servlets, which dispatch to JSPs using a very extensive custom tag library. This was all written pre-Struts, pre-JSTL, but you can assume it does all the same sorts of stuff -- conditionals, inclusion of data elements, iterations, etc. The tag library is very useful, and we consequently have pretty much zero Java code in our JSPs. This is proving especially valuable as I add a designer to my team; now I can just let him design stuff in the templating language and he doesn't need to dig into any Java code at all.




Now, I want to take all the reports we've previously done as HTML images on our site, and provide the ability to output them as other formats, such as PDF or Excel. Producing PDF and Excel documents from my data is not the problem -- I have libraries to do that. My problem is, what I want to do is make a JSP file looking something like this:



<pdf:document>
<pdf:p>
   Customer name: <portal:get name="customer.customerName"/>
</pdf:p>
</pdf:document>



So it's all custom tags and normal text. The "portal" tag library is my current tag library I use all over the site. The "pdf" tags are new tags I want to write that will build a PDF document up. Again, I can write the code to do this... my problem is, won't all the character content, like new lines and such, end up output to the browser through the JSPWriter? How do I suppress that output and just send out the PDF bytes I've built up?




I've considered putting a filter around all the whole servlet JSP request handling chain, and dumping whatever is in the output buffer... but that seems quite hackish. Can I do something in the doEndTag method of my pdf:document custom tag to skip outputting that content?




At first I thought there must be an easy way to do this... but so far, no dice. I guess I'm trying to circumvent the basic nice thing about JSPs -- you can make most of the document pure HTML, and just put the dynamic content in where you need it, knowing the JSP handler will pass through the rest of the text as-is.




So, any ideas? Every book I've consulted on the topic uses hacks like putting all the tags on one line so no embedded newlines and spaces exist to be output -- which pretty much ruins the utility of this approach in the first place (that is, letting my designer write up a JSP file that defines the PDF in an HTML-like syntax).