Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7190899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:36:53+00:00 2026-05-28T19:36:53+00:00

So normally in a Spring controller you’d just return a ModelAndView object and forward

  • 0

So normally in a Spring controller you’d just return a ModelAndView object and forward the request to the JSP.

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: callback("processed HTML from JSP");)

I know I could just use HttpClient to get the contents but was wondering if there’s a way to avoid that extra step by calling something like:

String contents = processJSP(modelAndView);

Updated for geek to show my final solution:

First you need a fake HttpResponse to hold the response:

@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();
        }
    }
}

DummyResponse

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<String> getHeaders(String arg0) { return null; }
    public Collection<String> 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) {}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:36:54+00:00Added an answer on May 28, 2026 at 7:36 pm

    Probably not possible easy way.
    Maybe injecting view resolver into controller and calling render with special response will help, but not sure :

    ViewResolver viewResoler = // injected
    View view = viewReslover.resolveViewName(String viewName, Locale locale);
    HttpServletResponse xresponse = // custom response, buffers data
    view.render(Map model, HttpServletRequest request, HttpServletResponse xresponse);
    String content = // extract conten from data from xresponse 
    

    ‘

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I normally just @Autowire things into spring objects. But I've encountered a situation where
Normally when you update an object in linq2sql you get the object from a
I have a command object associated with a spring form controller: public class PluginInstance
Normally you create a function using cfscript like: <cfscript> function foo() { return bar;
Normally I use imagecreatefromjpeg() and then getimagesize() , but with Firefox 3 I need
So normally I just put my sql connection string in my asp.net web.config and
Normally to return JSON from my Controllers methods, I add a @ResponseBody annotation and
I'm trying to write a web application using SpringMVC. Normally I'd just map some
I have a Spring 3.0 application, with an Web Controller Method. This method normaly
I'm used to Java EE but not Spring MVC. I've just created a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.