title: Custom 404 using Spring DispatcherServlet
body: <p>I've set up web.xml as below.  I also have an annotation-based controller, which takes in any URL pattern and then goes to the corresponding jsp (I've set that up in the -servlet.xml).  However, If I go to a page that ends in .html (and whose jsp doesn't exist), I don't see the custom 404 page (and see the below error in the log).  Any page that doesn't end in .html, I can see the custom 404 page.  </p>
  
  <p>How can I configure to have a custom 404 page for any page that goes through the DispatcherServlet? </p>
  
  <p>Also want to add that if I set my error page to a static page (ie. error.htm) it works, but if I change it to a jsp (ie. error.jsp), I get the IllegalStateException.  Any help would be appreciated.</p>
  
  <p><strong>log error</strong></p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response
  at org.apache.catalina.connector.Response.getWriter(Response.java:606)
  at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
  at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
  </code></pre>
  
  <p><strong>controller</strong></p>
  
  <pre><code>@RequestMapping(value = {"/**"})
  
  public ModelAndView test() {
  
      ModelAndView modelAndView = new ModelAndView();
  
      return modelAndView;
  }
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;servlet&gt;
   &lt;servlet-name&gt;my_servlet&lt;/servlet-name&gt;
   &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>&lt;servlet-mapping&gt;
      &lt;servlet-name&gt;my_servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>&lt;error-page&gt;
      &lt;error-code&gt;404&lt;/error-code&gt;
      &lt;location&gt;/error.html&lt;/location&gt;
  &lt;/error-page&gt;
  </code></pre>