Multiple button in one form JSP - Servlet
Hi there, today I will show one simple tutorial to show which button is clicked in one form.
The idea after I read this post on the forum how to find which button is clicked
Note : In this tutorial i will use this library :-
Here is the example
The idea after I read this post on the forum how to find which button is clicked
Note : In this tutorial i will use this library :-
- JSTL 1.1
Here is the example
The JSP File
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Multiple Buttom In Form</title>
</head>
<body>
<h1>Multiple Button In Form</h1>
${message}
<br/>
<form action="<c:url value="/servletMultipleButton" />" method="POST">
<input type="submit" name="BModify" value="Modify" ></input>
<input type="submit" name="BRemove" value="Remove" ></input>
<input type="submit" name="BSave" value="Save" ></input>
<input type="submit" name="BConfirm" value="Cancel" ></input>
<input type="submit" name="BReset" value="BReset" ></input> <br/>
<input type="submit" name="BModify" value="Modify2" ></input>
<input type="submit" name="BRemove" value="Remove2" ></input>
<input type="submit" name="BSave" value="Save2" ></input>
<input type="submit" name="BConfirm" value="Cancel2" ></input>
<input type="submit" name="BReset" value="BReset2" ></input>
</form>
</body>
</html>
The Servlet
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author coding noted
*/
public class servletMultipleButton extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String actionValue = "";
String button_selected = "";
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
button_selected = (String) en.nextElement();
actionValue = request.getParameter(button_selected);
}
request.setAttribute("message", "Button Clicked : " + button_selected + "<br/>" + "Button Value : " + actionValue);
request.getRequestDispatcher("/multipleButtonInForm.jsp").forward(request, response);
}
}
Web.xml
<servlet>
<servlet-name>servletMultipleButton</servlet-name>
<servlet-class>sys.servlet.servletMultipleButton</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletMultipleButton</servlet-name>
<url-pattern>/servletMultipleButton</url-pattern>
</servlet-mapping>
Output
References
- http://stackoverflow.com/questions/11830351/multiple-submit-buttons-in-the-same-form-calling-different-servlets
- http://stackoverflow.com/questions/6639340/can-i-have-two-submit-buttons-in-a-jsp-to-two-different-controllers