-1

i am learning Java EE and Maven and i am trying a simple dopost.but i am getting (HTTP method POST is not supported by this URL) what could be the issues login.jsp

<form action="/login" method="post" >
<div class="login-container less">
    <div class="well-login">
        <div class="control-group">
            <div class="controls">
                <div>
                    <input type="text" placeholder="Username or Email" class="login-input user-name">
                </div>
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <div>
                    <input type="password" placeholder="Password" class="login-input user-pass">
                </div>
            </div>
        </div>
        <div class="clearfix">
            <button class="btn btn-inverse login-btn" type="submit" value="submit">Login</button>
        </div>
        <div class="remember-me">
            <input class="rem_me" type="checkbox" value=""> Remeber Me
        </div>
    </div>
</div>
</form>

Login Servlet

package webapp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        super.doPost(request, response);
        response.getWriter().println("success");
    }
}

can not see where the error is coming from ? and every thing seem oky from my side SOS

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
lord-ivan
  • 63
  • 1
  • 3
  • 10

2 Answers2

4

Delete this line:

    super.doPost(request, response);

The doPost method in the HttpServlet base class is implemented to unconditionally return an HTTP error (405 for HTTP 1.1, 400 for HTTP 1.0). doGet, doPut and doDelete are all implemented in the same way.

If you want to your servlet to support POST requests, override doPost but do not call the superclass method.

Luke Woodward
  • 56,377
  • 16
  • 76
  • 100
  • can you explain little more about overriding super.doPost(request, response); throw Http error 405? – Bibek Shakya Sep 04 '16 at 13:01
  • 1
    @B'bekShakya: I don't entirely understand your question. If the question is 'why does the superclass method throw HTTP error 405', then the answer is because its implementers chose to do that. See Tomcat's implementation of HttpServlet [here](http://svn.apache.org/repos/asf/tomcat/trunk/java/javax/servlet/http/HttpServlet.java). As far as I can tell, the [HttpServlet javadoc](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServlet.html) doesn't say what the base class methods should do. – Luke Woodward Sep 04 '16 at 13:21
  • thank you sir, i want to be cleared about the base class method under the hood. like you said document doesn't really provide me that. – Bibek Shakya Sep 04 '16 at 13:31
0

when your page inside web-inf folder, resources which need to be protected from Http access are placed under WEB-INF and filter is attached to view (jsp/html/.doc/.txt/xml) to prevent direct access.

so use getServletContext() inside doGet()

request.getServletContext().getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);

and since you wont be navigating any other page from doPost() method so use include() method

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
    response.getWriter().println("<h1>success</h1>  ");
    request.getServletContext().getRequestDispatcher("/WEB-INF/views/login.jsp").include(request, response);
}

remove you override of super.doPost(request, response); inside dopost() and also <form action="login" method="post" > removing "/" from login

Bibek Shakya
  • 1,169
  • 1
  • 18
  • 41