wood burning stoves 2.0*
The moose likes JSP and the fly likes Caching rendered html in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Caching rendered html in jsp" Watch "Caching rendered html in jsp" New topic
Author

Caching rendered html in jsp

Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
Hello All,

My requirement is to capture the html that has been rendered in jsp and convert into PDF.
I wanted help as to how to access the rendered html from the jsp.

Also, in case there is an alternate way, I would be happy to know that too.

Thanks.
Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
This is just a wild guess, but maybe it'll work

write a servlet Filter that intercepts your servlet. in the doFilter() method capture the ServletOutputStream from the ServletResponse
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
Thanks for the reply.

Can you tell me how to read bytes from a response object?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

You can't. You'll probably need to implement a ResponseWrapper.

SearchFirst and you may find what you need.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
I tried to achieve it using a filter.
However I am stuck at the filtermapping in web.xml.

My url is as such:
objectHandle=Coding&actionHandle/initProjectForCoding&nextPage/coding/quickCode.grs

where object handle specifies the business object, and the action handle specifies the method to be executed. The nextpage denotes another controller call which generates the response.

This is the entry I have in web.xml


The way I have specified the filter mapping doesnt hit the filter.(I have a breakpoint in the filter and it never gets there).

Any help?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

First of all, that isn't a valid URL. And second, it doesn't even contain the string "codingGridController", so I don't know why you would expect the filter to be invoked for it.
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
The URl works. I do get the request from the jsp.

ProxyServlet&objectHandle=Coding&actionHandle/initProjectForCoding&nextPage/coding/quickCode.grs

The "/coding/quickCode.grs " points to a method in the codingGridController.

additional mappings:


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Paul Clapham wrote:... it doesn't even contain the string "codingGridController", so I don't know why you would expect the filter to be invoked for it.
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
I tried a different approach.
I am trying to use an Interceptor with ResponseWrapper to achieve this.

The interceptor postHandle method is invoked fine.
But the response wrapper isnt working well.
Any help?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

"Isn't working well"?

Pity about that. You might consider posting some details about why it isn't working well. Read this: ItDoesntWorkIsUseless if you don't see why you should post details.
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
I tried a totally different approach to achieve this.
I am using a java library to convert the html to pdf.

The pdf file is created fine but when I try to open it it says " the volume for a file has been externally altered so that the opened file is no longer valid"

Any idea why this is occuring?

Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

Actually this methodology won't work.
Simple way is to do by just store the output data into a object and write it into pdf response object.
Viidhya Kishore wrote:Hello All,

My requirement is to capture the html that has been rendered in jsp and convert into PDF.
I wanted help as to how to access the rendered html from the jsp.

Also, in case there is an alternate way, I would be happy to know that too.

Thanks.


Java Best Practices
Linux Best Practices
Amortization Calculator

Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
Thanks for your reply Vinoth.
Any code snippet to help?
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

Try using iText.jar for creating pdf document
M Jiva
Greenhorn

Joined: Sep 27, 2010
Posts: 8
I too want the same this, i.e to get the response in pdf format and without creating any intermediate file (which iText will create)..
please provide any example code if any of you have..
I think this can be done with streams but do not have a working code..
please help..
Viidhya Kishore
Ranch Hand

Joined: Jan 03, 2007
Posts: 99
I used a tool called wkhtmltopdf.
wkhtmltopdf

I started with getting the html of the page using document.documentElement.innerHTML
I then sent the html from the client to the server in Ajax call.
Retrieved the html at the server and wrote out a html file.

Then used wkhtmltopdf on it.
Html File : Save as q.html in the dir.


Create a dir and download wkhtmltopdf from the link provided. I am using version 0.99
Save the html file on that dir.
Now in command prompt, cd to that dir and then execute


Look in the dir now. You will see pdf. Neat right?
 
jQuery in Action, 2nd edition
 
subject: Caching rendered html in jsp
 
Similar Threads
HFSJ page 408.
Jquery-ajax request to Action class
h:command button not firing through java script
JSPs as XML documents, but no HTML rendered!
rendering PDf in a JSP page.