HTTP Status 405 – HTTP method is not supported by this URL

This is the default response of the default implementation of HttpServlet#doXxx() method (doGet(), doPost(), doHead(), doPut(), etc). This means that when the doXxx() method is not properly being @Overriden in your servlet class, or when it is explicitly being called via super, then you will face a HTTP 405 “Method not allowed” error.

So, you need to make sure that you have the doXxx() method properly declared conform the API, including the @Override annotation just to ensure that you didn’t make any typos. E.g.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // ...

And you also need to make sure that you don’t ever call super.doXxx() in your servlet method:

super.doGet(request, response);

Your servlet has this. Just get rid of this line and your problem shall disappear.

The HttpServlet basically follows the template method pattern where all non-overridden HTTP methods returns this HTTP 405 error “Method not supported”. When you override such a method, you should not call super method, because you would otherwise still get the HTTP 405 error. The same story goes on for your doPost() method.

This also applies on service() by the way, but that does technically not harm in this construct since you need it to let the default implementation execute the proper methods. Actually, the whole service() method is unnecessary for you, you can just remove the entire method from your servlet.

The super.init(); is also unnecessary. It’s is only necessary when you override the init(ServletConfig), because otherwise the ServletConfig wouldn’t be set. This is also explicitly mentioned in the javadoc. It’s the only method which requires a super call.

Unrelated to the concrete problem, spawning a thread in a servlet like that is a bad idea. For the correct approach, head to How to run a background task in a servlet based web application?

Leave a Comment