Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
112
rated 0 times [  115] [ 3]  / answers: 1 / hits: 16057  / 10 Years ago, sat, october 25, 2014, 12:00:00

i have a html form i want to call two different servlet on clicking two different button how to change the form action run time



<form>
<input type=submit value=Question Paper style=height:25px; width:120px; background-color: royalblue;color: white; />
<input type=submit value=Instruction style=height:25px; width:120px; background-color: royalblue;color: white; />);
</form>


on clicking the button i want to call servlet1 and servlet2 on each button



please help me to solve this problem, thanks in advance


More From » html

 Answers
45

There are several ways to achieve this.



Probably the easiest would be to use JavaScript to change the form's action.



<input type=submit value=SecondServlet onclick=form.action='SecondServlet';>


But this of course won't work when the enduser has JS disabled (mobile browsers, screenreaders, etc).



Another way is to put the second button in a different form, which may or may not be what you need, depending on the concrete functional requirement, which is not clear from the question at all.



<form action=FirstServlet method=Post>
Last Name: <input type=text name=lastName size=20>
<br><br>
<input type=submit value=FirstServlet>
</form>
<form action=SecondServlet method=Post>
<input type=submitvalue=SecondServlet>
</form>


Note that a form would on submit only send the input data contained in the very same form, not in the other form.



Again another way is to just create another single entry point servlet which delegates further to the right servlets (or preferably, the right business actions) depending on the button pressed (which is by itself available as a request parameter by its name):



<form action=MainServlet method=Post>
Last Name: <input type=text name=lastName size=20>
<br><br>
<input type=submit name=action value=FirstServlet>
<input type=submit name=action value=SecondServlet>
</form>


with the following in MainServlet



String action = request.getParameter(action);

if (FirstServlet.equals(action)) {
// Invoke FirstServlet's job here.
} else if (SecondServlet.equals(action)) {
// Invoke SecondServlet's job here.
}

[#69019] Wednesday, October 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsleyashlynnh

Total Points: 64
Total Questions: 119
Total Answers: 98

Location: Burundi
Member since Sat, Aug 21, 2021
3 Years ago
;