0
votes

I am developing a jsp application using eclipse. Currently trying to detect which button is pressed while submitting a form with ajax call. however the request.getParameter(button-name) returns null within the doPost method of servlet. below is the code for jsp, ajax call(jquery) and servlet. //jsp

<body>
    <form id="formCreateUser">
        <div>
            <table>
                <tr>
                    <th colspan="4"><label>New User Information</label></th>
                </tr>
                <tr>
                    <td><label>Name : </label> <input type="text" name="txtName"
                        id="txtName" /></td>
                    <td><label>Surname : </label><input type="text"
                        id="txtSurname" name="txtSurname" /></td>
                </tr>
                <tr>
                    <td><label>User name : </label><input type="text"
                        name="txtUserName" /></td>
                    <td><label>E-mail : </label> <input type="text"
                        name="txtEmail" /></td>
                </tr>
                <tr>
                    <td><label>Password : </label><input type="password"
                        id="txtPassword" /></td>
                    <td><label>Re-enter password : </label><input type="password"
                        id="txtPassword2" /></td>
                </tr>
                <tr>
                    <td><label>Is Admin ?</label> <input type="checkbox"
                        value="Admin" name="chkAdmin" id="chkAdmin" /></td>
                </tr>
            </table>
            <input type="button" value="Submit" name="btnSumbit" id="btnSubmit" />
        </div>
    </form>
</body>

//jquery

$(document).ready(function() {  
    $("#btnSubmit").click(function(e) {
        $.ajax({
            url : 'CreateUserServlet',
            type : 'POST',
            dataType : 'json',
            data : $("#formCreateUser").serialize(),
            success : function(data) {
                //something here
            }
        });
        return false;
    });     
}); 

servlet

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        if (request.getParameter("btnSubmit") != null) {
            System.out.println("It works");
        } else {
            System.out.println("It doesn't work");
        }
}

any help is highly appreciated Regards

1
$("#formCreateUser").serialize() will create a URL encoded text string by serializing form values, but not the buttons i guess try this data :{btnSumbit:$("#btnSubmit").val()} - techGaurdian
yes. but for OP's case it will work (as its not for multiple button currently) this will be just a start... - techGaurdian

1 Answers

0
votes

In the jquery part you must send the paramater and it's value, if you display the serialize() method it'wont give you the parameter of btnSubmit, so you must add it to the method like this: Replace this code :

data : $("#formCreateUser").serialize(),

By this one :

data : $("#formCreateUser").serialize()+"&btnSumbit="+$("#btnSubmit").val(),

i hope that it will work for you.