tags:

views:

41

answers:

2

How would I email the entire content of the JSP page output as an HTML or PDF email attachment?

Thanks

+1  A: 

Hello, You can first get the html source code with javascript.

var source = document.getElementsByTagName('html')[0].innerHTML

Then you can store this source in a hidden field and sent it when the user clicks a submit button. If you want to make it on page load send the source with ajax.

To ensure you get the full html source make sure you get it until the page is loaded.

Enrique
+1  A: 

If the request is idempotent (such as GET requests are), then just use java.net.URL to get an InputStream of the JSP output. E.g.

InputStream input = new URL("http://example.com/context/page.jsp").openStream();

If the request is not idempotent (such as POST requests are), then you need to create a Filter which wraps the ServletResponse with a custom implementation of the PrintWriter with the five write() methods been overridden wherein you copy output into some buffer/builder which you store in the session or a temporary folder at local disk file system so that it can be accessed afterwards in the subsequent requests. A code example can be found in the answer I posted before here.

To send it as an email, make use of JavaMail API or the more convenienced Apache Commons Email. Code examples can be found in their documentation/guide/FAQ.

If you'd like to convert the HTML to PDF first, then have a look at XhtmlRenderer. Code example can be found here.

BalusC