4

I need to make a particular service call and I need to pass a String object as a parameter that is HTML. This HTML is what will be rendered by that service ( this service sends emails and this HTML will be email body)

I have been using JSP files for all the templating so far. All my views that render HTML page and return it to users.

I don't know how I can execute a JSP file, fill in all the information and then convert it to text. This is my first time working with Spring MVC.

Edit: related question here is really outdated '09

sublime
  • 4,013
  • 9
  • 53
  • 92
  • Possible duplicate of [What would be the light way to render a JSP page without an App/Web Server](http://stackoverflow.com/questions/609405/what-would-be-the-light-way-to-render-a-jsp-page-without-an-app-web-server) – kryger Oct 23 '15 at 23:26
  • 2
    Bottom line: use a better templating engine that doesn't depend on servlet container, there are many. – kryger Oct 23 '15 at 23:29
  • @kryger I read this question but didn't really understand it. Do you mean to say I cannot execute JSP and then covert it to text? – sublime Oct 23 '15 at 23:36
  • If I understand this, you can use a Javascript function to get the HTML content after rendered with information. There's `document.head` to get HTML's head and `document.body` to get HTML's body. You can create a function to be executed when page is loaded. See http://www.w3schools.com/jsref/event_onload.asp – Raphael Amoedo Oct 27 '15 at 01:00
  • 2
    Use a different templating library like Thymeleaf and render the HTML in your handler. – Sotirios Delimanolis Oct 27 '15 at 01:05
  • As you may know JSP gets compiled to Java code by the servlet container, the resulting Java servlet code simply prints out the response via series of `out.println()` calls. To make use of a JSP you'd have to *programmatically* compile it to a Java servlet first. Yes, I meant to say "you cannot execute JSP and then covert it to text" *(...easily)*. Similar questions are old because JSP is half-dead, obsoleted by newer templating engines. – kryger Oct 28 '15 at 21:21
  • You can use JSPTest http://sourceforge.net/projects/jsptest/ to render jsp page. But it is little bit old. Maybe you can try this. – Pasupathi Rajamanickam Oct 29 '15 at 05:48

3 Answers3

1

I will focus on the part of converting your jsp to text.
There were some comments about the usage of jsp as templeting engine, but my suggestion is templeting agnostic.

The idea is to mimic a browser call, i.e. submit the form that we would like its result to be sent via email and make sure that we can access this page correctly via a browser.
I am assuming post request here change to get is easy.

public class MailServiceHelper {
    public String getJsp(String url, Map<String,String> form, HttpServletRequest request) {
        //we can figure out the base url from the request
        String baseUrl =""; 
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(baseUrl+"/"+url);
        for (String formElement : form.keySet()) {
            method.setParameter(formElement, form.get(formElement));    
        }

        try {

            int statusCode = client.executeMethod(method);
            if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES) {
                byte[] responseBody = method.getResponseBody();     
                return new String(responseBody,StandardCharsets.UTF_8);
            }else{
                throw new RuntimeException("Failed to read jsp, server returened with status code: "+statusCode);
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to read jsp",e);
        }finally{
            method.releaseConnection();
        }
    }

You may require authenticating your client which is supported as well.
I am using httpclient 3.1 in my exanple see http 3.1 authentication ;switching to newer client should be easy.

Sending the HTML by email is referred to in the answer below: How do I send an HTML email? It’s recommended to use internal Style Sheet so the email client will render the html correctly.

Community
  • 1
  • 1
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
1

Your scenario is really akin for a template engine that does not depend on the servlet container. Moreover, the stack that you are using pushes Thymeleaf as a top candidate.

If you're worried about adopting a new technology, you should know that there's already a blog and a github project showcasing exactly the email template scenario, so you'll have quite a good support for your own implementation

Master Slave
  • 27,771
  • 4
  • 57
  • 55
0

If you are in a controller or in general if you have access to the request you can use request.forward() with a Mock response that collects the written output and, when the forward returns you get it and continue processing.

final RequestDispatcher rd = request.getRequestDispatcher("myJsp.jsp");
final MockHttpServletResponse r = new MockHttpServletResponse();
rd.forward(request, r);
final String s = r.getContentAsString();

See MockHttpServletResponse

If the JSP expect parameters that are not in your request then you need to create a mock or a filter also for the request.

Testo Testini
  • 2,200
  • 18
  • 29