title: Equivalent to extending HttpServlet
body: <p>I got an spring web mvc architecture pice that I do not fully understand.  </p>
  
  <p>Java Code:  </p>
  
  <pre><code>public class ErrorHandler extends HttpServlet {
      public void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          out.println("&lt;!doctype html public \"-//w3c//dtd html 4.0 transitional//en\"&gt;");
          out.println("&lt;html&gt;&lt;head&gt;&lt;title&gt;ERROR&lt;/title&gt;&lt;/head&gt;&lt;body&gt;");
          out.println("&lt;h1&gt;An error occured&lt;/h1&gt;");
          out.println("&lt;/body&gt;&lt;/html&gt;");
      }
  }
  </code></pre>
  
  <p>web.xml  </p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;ErrorHandler&lt;/servlet-name&gt;
      &lt;servlet-class&gt;some.package.ErrorHandler&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;ErrorHandler&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/ErrorHandler&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;error-page&gt;
      &lt;error-code&gt;403&lt;/error-code&gt;
      &lt;location&gt;/ErrorHandler&lt;/location&gt;
  &lt;/error-page&gt;
  &lt;error-page&gt;
      &lt;error-code&gt;404&lt;/error-code&gt;
      &lt;location&gt;/ErrorHandler&lt;/location&gt;
  &lt;/error-page&gt;
  &lt;error-page&gt;
      &lt;exception-type&gt;java.lang.Throwable&lt;/exception-type&gt;
      &lt;location&gt;/ErrorHandler&lt;/location&gt;
  &lt;/error-page&gt;
  </code></pre>
  
  <p>The semantic of this code is clear, I guess: It is a error handler that catches HTTP status errors on the one hand (403 and 404) but also catches internal exceptions (eg. divide by 0, so internal 500 errors).  </p>
  
  <p>Now I whant to refactor it. That means that I want to create mostly java only code because the rest of the project is in this structure as well and I want it to be uniformly.<br>
  But here are my problems because from what I researched, it is not compleatly clear to me:  </p>
  
  <ol>
  <li>Why does someone here extended the <code>HttpServlet</code> class? Isn't this the same as a <code>@RequestMapping</code> annotation? But when I remove the <code>extends HttpServlet</code> and put a <code>@RequestMapping</code> to the <code>doGet</code> method, what do I have to set for the value? Or is there a mapping at all?<br>
  How can I get rid of the extension?  </li>
  <li>The <code>&lt;exception-type&gt;java.lang.Throwable&lt;/exception-type&gt;</code> should be equivalent to <code>@ExceptionHandler(value={Throwable.class})</code> with a <code>@RestControllerAdvice</code>. Or am I wrong here?  </li>
  <li>And what is with the <code>&lt;error-code&gt;404&lt;/error-code&gt;</code>? Is this equivalent to <code>@ExceptionHandler(value={ResourceNotFoundException.class})</code>? Or is it enouph that the Trowable.class is catched?  </li>
  <li>And am I missing something? The new code must be minimum as strong as the old one. So there must be at least the same functionality, but more is ok (ModelAndView as return instead of void and a PrintWriter. And moving the class to other packages without changing the web.xml, ....)  </li>
  </ol>
  
  <p>Thanks for any help :)</p>