How would I email the entire content of the JSP output as an HTML email attachment
How would I email the entire content of the JS开发者_JAVA百科P page output as an HTML or PDF email attachment?
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.
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.
精彩评论