问题
When <jsp:include>
is used for including HTML file DispatcherServlet
is throwing
java.lang.IllegalStateException: Cannot forward after response has been committed
I have one servlet:
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In it, I have enabled Spring MVC annotations and have handler mapping and adapter for JSP files without controllers (converting old webapp to Spring). And I have enabled DefaultServletHttpRequestHandler
in this Servlet.
Any idea how to avoid that IllegalStateException
when including html files?
回答1:
It is illegal to call forward() after some response is written to the output stream. The response may have been already sent to the client.
This article Causes of Response already committed explains why response is already committed.
回答2:
So if you let spring handle all html files, it will always fail on jsp:include
because spring cannot handle html includes.
Best way around this for me was to leave html files on default servlet.
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
and leave rest on DispatcherServlet
.
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
This is definitely not a best solution, but until I convert all jsps (around 1000 of them) to mvc and something like tiles this is the only way I can see it working.
来源:https://stackoverflow.com/questions/24239501/java-lang-illegalstateexception-cannot-forward-after-response-has-been-committe