java - retrieve button-name on jsp page servlet -
i developing jsp application using eclipse. trying detect button pressed while submitting form ajax call. request.getparameter(button-name) returns null within dopost method of servlet. below code jsp, ajax call(jquery) , 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 highly appreciated regards
in jquery part must send paramater , it's value, if display serialize() method it'wont give parameter of btnsubmit, must add method this: replace code :
data : $("#formcreateuser").serialize(),
by 1 :
data : $("#formcreateuser").serialize()+"&btnsumbit="+$("#btnsubmit").val(),
i hope work you.
Comments
Post a Comment