Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple submit buttons in the same form calling different Servlets

Tags:

java

jsp

servlets

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)

like image 673
Eli Katzav Avatar asked Aug 06 '12 14:08

Eli Katzav


People also ask

How do you handle multiple submit buttons in the same form?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Can you have multiple submit buttons in a form PHP?

Processing form data in PHP is significantly simpler than most other Web programming languages. This simplicity and ease of use makes it possible to do some fairly complex things with forms, including handling multiple submit buttons in the same form.


2 Answers

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="submit"value="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. } 

This is only not very i18n/maintenance friendly. What if you need to show buttons in a different language or change the button values while forgetting to take the servlet code into account?


A slight change is to give the buttons its own fixed and unique name, so that its presence as request parameter could be checked instead of its value which would be sensitive to i18n/maintenance:

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

with the following in MainServlet

if (request.getParameter("first") != null) {     // Invoke FirstServlet's job here. } else if (request.getParameter("second") != null) {     // Invoke SecondServlet's job here. } 

Last way would be to just use a MVC framework like JSF so that you can directly bind javabean methods to buttons, but that would require drastic changes to your existing code.

<h:form>     Last Name: <h:inputText value="#{bean.lastName}" size="20" />     <br/><br/>     <h:commandButton value="First" action="#{bean.first}" />     <h:commandButton value="Second" action="#{bean.Second}" /> </h:form> 

with just the following javabean instead of a servlet

@ManagedBean @RequestScoped public class Bean {      private String lastName; // +getter+setter      public void first() {         // Invoke original FirstServlet's job here.     }      public void second() {         // Invoke original SecondServlet's job here.     }  } 
like image 116
BalusC Avatar answered Sep 30 '22 14:09

BalusC


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> 
like image 23
kiril Avatar answered Sep 30 '22 13:09

kiril