All the HTML forms so far used contains only one submit button. But yet many times, it will be necessary to have multiple submit buttons in a single HTML form. User is at choice to click any one submit button. Now the question is how the Servlet knows from which submit button the request came or to say, how the Servlet knows which submit button user clicked because all the submit buttons have only one ACTION attribute calling the same Servlet. It is required by the Servlet, as per the submit clicked, the appropriate response is to be sent to client.
One example is the HTML form may have OK button, Retry button or Cancel button (three submit buttons). It is shown earlier one example with multiple submit buttons in Cookies Simple Shopping Cart Example.
First observe the HTML form, then we will go into details of coding.
HTML file with two submit buttons: MultipleSubmitForms.html
Using Multiple Submit Buttons in a Single Form
Generally with one submit button, name attribute will not be given. With multiple submit buttons it is very necessary to give the names and on these names the Servlet will recognize from which submit the request arrived. In the above form, two submit buttons with names p1 and a1 exist with values
PERIMETER and AREA.
web.xml entry for Servlet
snrao1
MultipleSubmitButtons
snrao1
/MSB
Servlet reading two submit buttons: MultipleSubmitForms.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MultipleSubmitButtons extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType( "text/html" ) ;
ServletOutputStream sos = res.getOutputStream(); // instead of PrintWriter
// reading from text field
double radius = Double.parseDouble(req.getParameter("rad"));
// reading from submit buttons
String str1 = req.getParameter("a1"); // AREA submit button
String str2 = req.getParameter("p1"); // PERIMETER submit button
sos.println("");
if(str1 != null) // if AREA is clicked
{
sos.println("You clicked " + str1 + " submit button
");
sos.println("Area is "+ Math.PI*radius*radius+"
");
}
else if(str2 != null) // if PERIMETER is clicked
{
sos.println("You clicked " + str2 + " submit button
");
sos.println("Perimeter is "+ 2*Math.PI*radius+"
") ;
}
sos.println("");
sos.close( );
}
}
ServletOutputStream sos = res.getOutputStream();
Just for difference, instead of PrintWriter, the ServletOutputStream is used. This we discuss separately.
String str1 = req.getParameter("a1"); // AREA submit button
String str2 = req.getParameter("p1"); // PERIMETER
If the user clicks a1 submit button, the string str1 will be AREA and str2 will be null. Similarly, if the user clicks p1 submit button, the string str2 will be PERIMETER and str1 will be null. That is, if the form contains multiple submit buttons, only one string will be not null and all other will be null. On the "not null value", the appropriate response may be forwarded to client.
Client HTML file entered with radius 4.56

Output screen when PERIMETER submit button is clicked (of Multiple Submit Buttons)
Output screen when AREA submit button is clicked.