First, here is the code:
<form action="FirstServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" value="FirstServlet">
<input type="submit"value="SecondServlet">
</form>
I'd like to understand how to send information in case the FirstServlet button
was pressed to FirstServlet
and in case the SecondServlet button
was pressed to SecondServlet
.
important:
I want to do it in the same form so the same information would be transered to both the servlets. (of course, in the servlets I'll use the info accordingly)
If you use spring this a better way
Is there any way to create form with multiple submit buttons on Spring MVC using annotations?
In addition to the previous response, the best option to submit a form with different buttons without language problems is actually using a button tag.
<form>
...
<button type="submit" name="submit" value="servlet1">Go to 1st Servlet</button>
<button type="submit" name="submit" value="servlet2">Go to 2nd Servlet</button>
</form>
function gotofirst(){
window.location = "firstServelet.java";
}
function gotosecond(){
window.location = "secondServelet.java";
}
<form action="FirstServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" onclick="gotofirst()" value="FirstServlet">
<input type="submit" onclick="gotosecond()" value="SecondServlet">
</form>
If you use jQuery, u can do it like this:
<form action="example" method="post" id="loginform">
...
<input id="btnin" type="button" value="login"/>
<input id="btnreg" type="button" value="regist"/>
</form>
And js will be:
$("#btnin").click(function(){
$("#loginform").attr("action", "user_login");
$("#loginform").submit();
}
$("#btnreg").click(function(){
$("#loginform").attr("action", "user_regist");
$("#loginform").submit();
}
You may need to write a javascript for each button submit. Instead of defining action in form definition, set those values in javascript. Something like below.
function callButton1(form, yourServ)
{
form.action = yourServ;
form.submit();
});
Source: Stackoverflow.com