• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing the output of JSP into a string ?

 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a servlet that needs to capture the output of some JSP into a (rather large) String.
Both the JSP and the servlet reside on the same web application.

For instance, if my JSP were:
<html><body><%=2+2%></body></html>
My servlet should load it into the String:
"<html><body>4</body></html>"


Is there an easy way to achieve this ?
(It probably seems twisted and inefficient, but we need it for some ancient API).

I've considered letting the servlet open a new URL connection to the JSP (so that the servelt will imitate the behavior of a browser). But it seems cumbersome to open a new TCP connection, when the JSP is already on the same webapp...

Thanks.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats really strange. Why would you want to do this ? I dont know of any other way other than to programatically invoke the JSP, but why ?
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

*Sigh* like most programming nightmares, it's all about "existing code with no one can touch"...

My team is maintaing an application written for the Vignette platform. Vignette offers this "jsp to string" functionality, and the previous team used it a lot (can't imaging why).

Now we're migrating into standard Weblogic, and we're not renewing the Vignette license. So we're hoping to "simulate" the API, in order to avoid re-writing any logic. Rewriting would be painful, because the app is huge and horribly written (like, they would take those poor strings, stick them into Hashtables, move them between session and request scope, and whatnot).

thanks anyway.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good lord, I feel for you! What a mess. (And having been exposed to Vignette in the past, double condolances!)

At what point in the request/response cycle do you need to capture the JSP output?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you wrap the response object and override a few methods, you can capture the output stream to do what you like with it.
Here is a small app that does this (writes the output to stdout).
http://simple.souther.us/capture.war

Whether or not this will be useful to you depends on the answer to Bear's question.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holy cow !!! heheheh. I thought one of the applications a co worker was maintaining was bad. All the html and JSP code is written inside a java script file. So its like document.write() a couple of million times. hehe

If you wrap the response object and override a few methods



Yep ! Agree there. If the case is a generic condition where the output JSP would have to be saved everytime perhaps you could throw in a filter. It depends on when you want this stringified JSP page. My deepest condolences to you
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much for the replies & comisseration.

John Meyers, this co-worker sounds like he could taken on many of thedailyWTF finest ;)

The JSPs should be captured on incoming requests: the Servlet's doGet/doPost would capture some JSPs, then use them as fragements for building the HTML response.
So, I suspect a filter would be a bit tricky to implement here - perhaps we'll open new URL connections after all, and live with the performance tradeoff.

Thanks again for those professional advices.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic