2

I have been trying this for a while with no luck. I want to know if there is a way to include response of a server request (i.e. a Spring controller) inside a JSP.

I am trying to build an independent module which will rely on some specific objects and will print the HTML based on these objects. So I want to create a Controller method which will take care of dependencies and return this JSP as response. I can then include a call in the parent JSP so that it hits the controller method and injects the response returned by this method in the parent JSP.

I read somewhere that jsp:include can be used for this purpose as follows: <jsp:include page="/test-url" flush="true"/>

Where /test-url will map to a Spring controller method.

But when I run this, i get the following exception:

Servlet.service() for servlet dispatcher threw exception: java.lang.IllegalStateException: Cannot forward after response has been committed

Please provide your valuable inputs if you have some idea about this?

1 Answers1

0

use this way

String pPath = yoururl 
<jsp:include page="<%=pPath%>" flush="true">

it work fine for me

Alaa Abuzaghleh
  • 1,023
  • 6
  • 11
  • Hmmm. Tried this as well. but there isn't much difference in this and my earlier approach where i am giving the string directly in the page attribute. Can you please let me know if you are also using Spring? And what format are your URLs, i mean do we need to add any prefix like the project name? – Kunwardeep Singh May 01 '15 at 21:58
  • let me ask you one thing, what is the return from your controller String , or ModelAndView, or JSON – Alaa Abuzaghleh May 01 '15 at 22:02
  • Its ModelAndView but it doesn't even hit the controller. Rather it throws the error when it executes this line in the parent JSP. – Kunwardeep Singh May 01 '15 at 22:09
  • http://stackoverflow.com/questions/24239501/java-lang-illegalstateexception-cannot-forward-after-response-has-been-committe is same problem you have – Alaa Abuzaghleh May 01 '15 at 22:15
  • So basically I cant hit a controller method via jsp:include? – Kunwardeep Singh May 01 '15 at 23:15
  • yes hit the modelandview get your data and then include other jsp page withen the current request to show your data – Alaa Abuzaghleh May 01 '15 at 23:24
  • Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long but it is expected. – peterh May 02 '15 at 00:10