2

Till now I believed it was a common practice to call a super.doPost(req, resp) from your servlet's doPost(req, resp){} But here's the issue I faced - I have a very simple servlet having a doPost(req, resp) and since it was auto generated method from eclipse it was having super.doPost(req, resp) which is fine as it is calling its parent's doPost() but I was getting

HTTP Status 405 - HTTP method GET is not supported by this URL whenever the servlet was hit. I went through a lot of post and this post

had been talking about the same issue and one of the solution suggested was remove the super.doGet().

I did the same with my Post method and to my surprise it worked!!!!

I cannot find a logical reason for this. Can someone please explain what was happening? Why was

405 flashing due to call of super.doPost().

Thanks, Saurabh.

Community
  • 1
  • 1
Saurabh
  • 85
  • 2
  • 10

1 Answers1

3

The default implementation of HttpServlet.doPost returns a 405 error (method not allowed). You have to implement the doPost method if you want to support the POST method in your servlet.

This is the code of HttpServlet.doPost:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String protocol = req.getProtocol();
    String msg = lStrings.getString("http.method_post_not_supported");
    if (protocol.endsWith("1.1")) {
        resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
    } else {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
    }
}
LaurentG
  • 11,128
  • 9
  • 51
  • 66
  • So we extend the HttpServlet and @Override doPost but in our implementation we don't call its super since call to the super will give this message. Hmmm.... After reading your reply I also went through [link](http://stackoverflow.com/questions/10244785/when-not-to-call-super-method-when-overriding). Got It!!! – Saurabh Nov 23 '13 at 08:00