Friday 18 October 2013

Multiple button in one form JSP - SERVLET

Leave a Comment
Hye there, today i will show one simple tutorial to show that which button is clicked in one form.
The idea after i read this post on 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

  1. http://stackoverflow.com/questions/11830351/multiple-submit-buttons-in-the-same-form-calling-different-servlets
  2. http://stackoverflow.com/questions/6639340/can-i-have-two-submit-buttons-in-a-jsp-to-two-different-controllers


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.