springjspservletsjspinclude

java.lang.IllegalStateException: Cannot forward after response has been committed while including HTML files using jsp:include


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?


Solution

  • 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.