silver February 2016

Parameters passed to URL not working

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");
    }
}

Answers


wero February 2016

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


Perdomoff February 2016

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?

Post Status

Asked in February 2016
Viewed 2,529 times
Voted 10
Answered 2 times

Search




Leave an answer