• Post Reply Bookmark Topic Watch Topic
  • New Topic

Templates

 
basha khan
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I got a problom,

How can i get the output of a JSP page into a java class?.That is,The original JSP page sends the generated HTML to the browser.What i need is that generated HTML into a java class.

Suppose i got an Email class ,

String myContent = -------
Email.setContent(mycontent);

The string myContent should be the dynamically generated HTML from a JSP page.

that is,

String myContent = output from CustInfo.jsp.

How to achieve this?.

Thanks,
Basha
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The proper way is to place an HTTP call to the JSP and capture the output in a stream or stringbuffer.
There may be other ways by hacking into the application server and getting the output directly but I've never tried those.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could catch the output stream with a filter and then substitute some other text in it's place:
IE: "Your mail has been sent".
 
basha khan
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jearon and Ben,Sorry,i was out of station for 10 days.Still some questions,

Originally posted by Jeroen Wenting:
The proper way is to place an HTTP call to the JSP and capture the output in a stream or stringbuffer.


What's the method to make an HTTP call and store output in a StringBuffer?.

There may be other ways by hacking into the application server and getting the output directly but I've never tried those.


I think it's a common problom.And i believe there should be a method in servlet API to achieve this.Anybody tried?.

Originally posted by Ben Souther:
You could catch the output stream with a filter and then substitute some other text in it's place:
IE: "Your mail has been sent".


I dont want to replace content.I just want to use the JSP file as a template to generate custom HTML.As this,

String myContent = output from CustInfo.jsp.
Email.setContent(mycontent);

So email will be an HTML email generated from a JSP.

Thanks
Basha
[ March 13, 2005: Message edited by: basha khan ]
 
basha khan
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys,

Any working answer for me?.I dont got a solution yet.

Thanks,
Basha
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a real quick and dirty app that I wrote a while back.
http://simple.souther.us/capture.war

It just takes the output from a JSP and prints it to stdout (using the "wrapper" pattern). You could easily use the same tecnique to pass the text to some other class.
 
basha khan
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben,

I'll try and tell u the result.
 
Dan Novik
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several taglibs in Coldtags suite:
Copy, Request, HTTP
See http://www.servletsuite.com/jsp.htm
 
basha khan
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Here's a real quick and dirty app that I wrote a while back.
http://simple.souther.us/capture.war

It just takes the output from a JSP and prints it to stdout (using the "wrapper" pattern). You could easily use the same tecnique to pass the text to some other class.


Hi Ben,



The class ResponseWrapper acts when browser sends a request to the page.In ur test app,browser sends request to index.jsp,so server creates an OutputStream and appends data to it.Then u use a wrapper class ro wrap the stream and use that stream to print output to the console.

But in my case,Thing is little different,i have to call the JSP to generate HTML from a java class.Not from browser.My app contains many JSP's also.I m not understanding how can i call a JSP file from a java class.

The problom is fairly simple and a regular problom i think.It'll giv a gr8 template pattern to create custom HTML.I think API method should be there.

Any other HTML code generation pattern other than using JSP?.I can hard code the HTML code generation in a class.But i think readymade solution should be there.

Thanks,
Basha
 
David Cox
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Open a URLConnection (or HttpURLConnection) to CustInfo.jsp,
use the getInputStream() method to get a reference to the input stream for the jsp,
read this stream (which will be the HTML for CustInfo.jsp) into the String.
[ March 23, 2005: Message edited by: David Cox ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David's advice is good.
You can also look at the httpclient from
http://jakarta.apache.org/commons/httpclient
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!