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.
Asked
Active
Viewed 2,030 times
2 Answers
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:
-
Thanks. Exactly what i need. And yes I agree but that is the way the assignment is laid out. – user2077348 Feb 16 '13 at 03:33
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