I have a batch job that runs outside any request, response scope.
I would like to be able to define the email content in JSP since the email content is primarly an HTML开发者_开发知识库 document with placeholders for values.
Basically, I would like to be able to run a JSP from a static method.
Is this possible? The solutions online suggests having access to a request and response, implementing httpservlet. Can I not fake these?
I know this is a difficult question, but it would feel good to find a solution for it.
JSP as a view technology is inseparably tied to the HTTP request/response cycle. You're better off using some other templating engine such as FreeMarker to generate the e-mail message body.
You should be able to fire just a HTTP request on the JSP and get its HTTP response.
InputStream response = new URL("http://localhost/context/page.jsp").openStream();
// ...
But, JSP is definitely the wrong tool for the job. You don't need to send an unnecessary HTTP request. Rather use a template engine or make use of a plan text/html file with MessageFormat
API.
精彩评论