0

I use Spring MVC. I want to render some jsp by forwarding to it. And then I want to write the result to json.

For exmple I want to render my complex jsp and on exit I want to get: {"result":"ok","html":"......."}

How can I do this? I've tried to look at request.getRequestDispatcher("tutorMini").forward(request, response) But if I can't pass response to it, bcz it should write all output to it.

And I've tried to use some json tags in jsp, but it has some troubles with hierarchy: HTML output with jsp:include and json-taglib

1 Answer 1

0

Since you need to apply additional conversion when inserting HTML into JSON (escape ' and "), you cannot write output of your JSP to the response directly.

So, you need to create an instance of ServletResponseWrapper that would save the output (by overriding getWriter() and/or getOutputStream()) and pass it to RequestDispatcher.include() (it looks more appropriate than forward() for this case):

MyServletResponseWrapper wrapper = new MyServletResponseWrapper(response);
request.getRequestDispatcher("tutorMini").include(request, wrapper);
String html = wrapper.getSavedOutput();

Then you can insert the saved content into JSON, escaping it appropriately.

Sign up to request clarification or add additional context in comments.

2 Comments

And what should I write in MyServletResponseWrapper? Is there any fake implementation of it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.