• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing the output of JSP into a string ?

 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • 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.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!