2

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?

Tiny
  • 27,221
  • 105
  • 339
  • 599
Pavel Polivka
  • 858
  • 4
  • 22

3 Answers3

1

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.

Pavel Polivka
  • 858
  • 4
  • 22
0

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.

Community
  • 1
  • 1
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
0

I tried the proposed solution which declares the default servlet mapping for *.html url patterns and it worked fine. The only problem was that it introduced some side effects in my case (an hybrid webapp, spring and non-spring managed): html files that should have been managed by Spring's front controller now were managed by Tomcat's default controller.
Fortunately, I found a couple of solutions with zero impact on the rest of the webapp.

  1. Use .jsp file extension instead of .html. Spring won't complain if it finds <jsp:include page="file.jsp" /> instead of <jsp:include page="file.html" />
  2. Include the .html file in a scriptlet <%=file.html %> and avoid using the jsp "include" tag
lainatnavi
  • 1,453
  • 1
  • 14
  • 22