javascript - How to send parameters from ajax to servlet -


i trying add 2 numbers using servlets , ajax/javascript. getting java.lang.numberformatexception: , values null. can know how pass parameters ajax servlet.

sumwithajaxservlet.java

public class sumwithajaxservlet extends httpservlet  {      protected void dopost(httpservletrequest request,httpservletresponse response)throws servletexception,ioexception         {             printwriter out = response.getwriter();              system.out.println("n1 : "+request.getparameter("n1"));             system.out.println("n2 : "+request.getparameter("n2"));              int num1 = integer.parseint(request.getparameter("n1"));             int num2 = integer.parseint(request.getparameter("n2"));             out.println(num1+num2+"");         } } 

index.jsp

<script type="text/javascript">   function calc()  {      var xmlhttp = new xmlhttprequest();      var value1 = document.getelementbyid("n1").value;      var value2 = document.getelementbyid("n2").value;       xmlhttp.open("post", "sumwithajaxservlet", true);      xmlhttp.send(value1 + "," + value2);       var result = document.getelementbyid("result");      result.innerhtml = xmlhttp.responsetext;  } </script>  <body> <form id='calcform'>         <table border="3">             <tr>                 <td>enter 1st number :</td>                 <td><input type="text" name="n1" id="n1"></td>             </tr>             <tr>                 <td>enter 2nd number :</td>                 <td><input type="text" name="n2" id="n2"></td>             </tr>             <tr>                 <td>result :</td>                 <td><input type="text" value="" id="result"></td>             </tr>             <tr>                 <td>&nbsp;</td>                 <td><input type="button" id="calculate" value="calculate"                     onclick="calc()" /></td>             </tr>         </table>     </form> </body> 

web.xml

<?xml version="1.0" encoding="utf-8"?> <web-app>      <servlet>         <servlet-name>sumwithajaxservlet</servlet-name>         <servlet-class>sumwithajaxservlet</servlet-class>     </servlet>      <servlet-mapping>             <servlet-name>sumwithajaxservlet</servlet-name>             <url-pattern>/sumwithajaxservlet</url-pattern>     </servlet-mapping>     <welcome-file-list>           <welcome-file>index.jsp</welcome-file>      </welcome-file-list>   </web-app> 

you sending data using non-standard, custom encoding, trying parse if encoded using standard form url encoding.

given, sake of example, values 333 , 555: sending string 333,555. need send string n1=333&n2=555.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -