0

When I input values into the form, it is going through the if-else accordingly.

However, when I pass parameters via the URL like this:

http://localhost:8080/sessiondemo/index.html?user=wronguser&pass=wrongpass

It is not going through anything. None of the println's are executing (not even the user/pass/end). It just goes back to the form.

May I know why is this so?

<form action = "processlogin.cgi" method = "post">
    <p>Username: <input type = "text" name = "user" size = "25"/></p>
    <p>Password: <input type = "password" name = "pass" size = "25"/></p>
    <input type = "submit" value = "Login"/>
</form>



@WebServlet("/processlogin.cgi")
public class ProcessLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user = request.getParameter("user");
        String pass = request.getParameter("pass");

        System.out.println("User: " + user);
        System.out.println("Pass: " + pass);

        if(UserManager.isValidUser(user, pass)) {
            System.out.println("valid");

        } else {
            System.out.println("invalid")
        }

        System.out.println("end");
    }
}
silver
  • 4,573
  • 12
  • 52
  • 82
  • 1
    If you pass your parameters via an url, it is the HttpGet method, not HttpPost. You'll have to overwrite `doGet`, too. – Jörn Buitink Feb 08 '16 at 14:29

2 Answers2

2

Your servlet only handles POST requests.

When you enter an URL like

http://localhost:8080/sessiondemo/index.html?user=wronguser&pass=wrongpass

or set the method in your HTML form to GET you are generating a GET request.

To also handle GET requests you need to override HttpServlet.doGet or HttpServlet-service.

The code you are using in your doPost method works fine for GET requests.

UPDATE:

Your servlet is registered for path /processlogin.cgi therefore to test a GET call from the browsers you need to enter

http://localhost:8080/sessiondemo/processlogin.cgi?user=wronguser&pass=wrongpass
wero
  • 30,527
  • 3
  • 46
  • 72
  • Hello, I have changed it to `method = "get"` and copied the same code in the `doGet()` but results are the same. – silver Feb 08 '16 at 14:38
  • @silver then calling index.html does not hit your servlet. – wero Feb 08 '16 at 14:39
  • Oh so that's what you meant by "not hitting the Servlet". I was passing to the html itself. Silly me. Thank you, accept and upvote for you. :) – silver Feb 08 '16 at 14:56
2

If url parameters are in the equation and you're retrieving them in this form request.getParameter(""), you should be doing your work in/overriding the get method, that is the type of request you are trying to generate.

That being said, a little more insight here:

How are parameters sent in an HTTP POST request?

Community
  • 1
  • 1
Perdomoff
  • 921
  • 2
  • 7
  • 24