0

I have two jsp pages both consisting of forms. One is a "signup.jsp" page the other "login.jsp". Both form action is sent to "Validate" servlet. How would the servlet determine which form has been submitted, so it will be validated corresponding to which the user submits.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2077348
  • 7
  • 1
  • 6

2 Answers2

4

Give the submit button an unique name.

<form ...>
    ...
    <input type="submit" name="signup" value="Signup" />
</form>
<form ...>
    ...
    <input type="submit" name="login" value="Login" />
</form>

Like with every other input element, their name=value pair will also be sent as request parameter. So, all you need to do in the servlet is just

if (request.getParameter("signup") != null) {
    // Signup form is submitted.
}
else if (request.getParameter("login") != null) {
    // Login form is submitted.
}

Unrelated to the concrete problem, this design is however somewhat strange. I'd expect a separate servlet for each form. Whatever code you've in "Validate" servlet should be refactored to a standalone class which you could just import/use in the both servlets.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

An alternative to BalusC answer:

<form ...>
    ...
    <input type="submit" name="signup" value="signup" />
</form>

<form ...>
    ...
    <input type="submit" name="signup" value="login" />
</form>

If I understand correctly Java is "pass by value" so in the servlet you can use switch/case to check the parameters value:

String signup = request.getParameter("signup");
switch(signup){
  case "signup":
    // signup form is submitted
  break;
  case "login":
    // login form is submitted
  break;
}
crmepham
  • 4,676
  • 19
  • 80
  • 155