title: get contents of processed JSP into spring controller without using HttpClient?
body: <p>So normally in a Spring controller you'd just return a <code>ModelAndView</code> object and forward the request to the JSP.</p>
  
  <p>What I need to do is actually get the contents of that processed JSP so I can then send it in a JSONP response (ex: <code>callback("processed HTML from JSP");</code>)</p>
  
  <p>I know I could just use <code>HttpClient</code> to get the contents but was wondering if there's a way to avoid that extra step by calling something like:</p>
  
  <pre><code>String contents = processJSP(modelAndView);
  </code></pre>
  
  <p><br/><br/><br/>
  <em>Updated for geek to show my final solution:</em></p>
  
  <p>First you need a fake HttpResponse to hold the response:</p>
  
  <pre><code>@Service
  public class SpringUtils {
      private static final Logger LOG = Logger.getLogger(SpringUtils.class);
  
      @Autowired private ViewResolver viewResolver;
      @Autowired private LocaleResolver localeResolver;
  
      public String renderView(HttpServletRequest request, ModelAndView mav) {
          try {
              View view = viewResolver.resolveViewName(mav.getViewName(), localeResolver.resolveLocale(request));
              HttpServletResponse localResponse = new MyHttpServletResponseWrapper(new DummyResponse());
  
              view.render(mav.getModel(), request, localResponse);
  
              return localResponse.toString();
          } catch (Exception e) {
              return "";
          }
      }
  
      public boolean doesViewExist(HttpServletRequest request, String viewName) {
          try {
              if (viewResolver.resolveViewName(viewName, localeResolver.resolveLocale(request)) != null) {
                  return true;
              }
          } catch (Exception e) {
              LOG.error(e.getMessage(), e);
          }
  
          return false;
      }
  
      static class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {
          private StringWriter sw = new StringWriter();
  
          public MyHttpServletResponseWrapper(HttpServletResponse response) {
              super(response);
          }
  
          public PrintWriter getWriter() throws IOException {
              return new PrintWriter(sw);
          }
  
          public ServletOutputStream getOutputStream() throws IOException {
              throw new UnsupportedOperationException();
          }
  
          public String toString() {
              return sw.toString();
          }
      }
  }
  </code></pre>
  
  <p><br/><br/><br/><br/>
  DummyResponse</p>
  
  <pre><code>public class DummyResponse implements HttpServletResponse {
      public DummyResponse() {
      }
  
      public void setAppCommitted(boolean appCommitted) {}
      public boolean isAppCommitted() { return false; }
      public int getContentCount() { return -1; }
      public boolean getIncluded() { return false; }
      public void setIncluded(boolean included) {}
      public String getInfo() { return null; }
      public ServletResponse getResponse() { return null; }
      public OutputStream getStream() { return null; }
      public void setStream(OutputStream stream) {}
      public void setSuspended(boolean suspended) {}
      public boolean isSuspended() { return false; }
      public void setError() {}
      public boolean isError() { return false; }
      public ServletOutputStream createOutputStream() throws IOException {
          return null;
      }
      public void finishResponse() throws IOException {}
      public int getContentLength() { return -1; }
      public String getContentType() { return null; }
      public PrintWriter getReporter() { return null; }
      public void recycle() {}
      public void write(int b) throws IOException {}
      public void write(byte b[]) throws IOException {}
      public void write(byte b[], int off, int len) throws IOException {}
      public void flushBuffer() throws IOException {}
      public int getBufferSize() { return -1; }
      public String getCharacterEncoding() { return null; }
      public void setCharacterEncoding(String charEncoding) {}
      public ServletOutputStream getOutputStream() throws IOException {
          return null;
      }
      public Locale getLocale() { return null; }
      public PrintWriter getWriter() throws IOException { return null; }
      public boolean isCommitted() { return false; }
      public void reset() {}
      public void resetBuffer() {}
      public void setBufferSize(int size) {}
      public void setContentLength(int length) {}
      public void setContentType(String type) {}
      public void setLocale(Locale locale) {}
  
      public Cookie[] getCookies() { return null; }
      public String getHeader(String name) { return null; }
      public Collection&lt;String&gt; getHeaders(String arg0) { return null; }
      public Collection&lt;String&gt; getHeaderNames() { return null; };
      public String[] getHeaderValues(String name) { return null; }
      public String getMessage() { return null; }
      public int getStatus() { return -1; }
      public void reset(int status, String message) {}
      public void addCookie(Cookie cookie) {}
      public void addDateHeader(String name, long value) {}
      public void addHeader(String name, String value) {}
      public void addIntHeader(String name, int value) {}
      public boolean containsHeader(String name) { return false; }
      public String encodeRedirectURL(String url) { return null; }
      public String encodeRedirectUrl(String url) { return null; }
      public String encodeURL(String url) { return null; }
      public String encodeUrl(String url) { return null; }
      public void sendAcknowledgement() throws IOException {}
      public void sendError(int status) throws IOException {}
      public void sendError(int status, String message) throws IOException {}
      public void sendRedirect(String location) throws IOException {}
      public void setDateHeader(String name, long value) {}
      public void setHeader(String name, String value) {}
      public void setIntHeader(String name, int value) {}
      public void setStatus(int status) {}
      public void setStatus(int status, String message) {}
  }
  </code></pre>