Вопрос

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.

Это было полезно?

Решение

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);
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top