In this article, I will continue to explain the second application, and make a simple addition calculator, which also uses $. ajax (), $. get (), $. the post () method is implemented separately. Different from the previous method, you need to enter the number to be calculated from the foreground and return the calculation result to the foreground page.
Paste the HTML page.ArticleAfter reading this article, it will be very easy:
<SCRIPT type = "text/JavaScript" src = "JS/jquery. JS "> </SCRIPT> <title> jquery and Ajax complete a simple addition calculator </title> <SCRIPT type =" text/JavaScript "> $ (document ). ready (function () {$ ("# btn1 "). click (function () {$. ajax ({type: "Get", URL: "computeservlet", datatype: "html", data: {num1 :$ ("# num1 "). val (), num2: $ ("# num2 "). val ()}, success: function (PARAM) {$ ("# result "). val (PARAM) ;}}) ;}; $ ("# btn2 "). click (function () {$. ajax ({type: "Post", URL: "computeservlet", datatype: "html", data: {num1 :$ ("# num1 "). val (), num2: $ ("# num2 "). val ()}, success: function (PARAM) {$ ("# result "). val (PARAM) ;}}) ;}; $ ("# btn3 "). click (function () {$. get ("computeservlet", {num1: $ ("# num1 "). val (), num2: $ ("# num2 "). val ()}, function (Params) {$ ("# result "). val (Params) ;}) ;}; $ ("# btn4 "). click (function () {$. post ("computeservlet", {num1: $ ("# num1 "). val (), num2: $ ("# num2 "). val ()}, function (Params) {$ ("# result "). val (Params );});});}); </SCRIPT>
The running interface is as follows:
The servlet is as follows:
Public class computeservlet extends httpservlet {public void destroy () {super. destroy ();} public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {system. out. println ("Get ++"); response. setcontenttype ("text/html"); printwriter out = response. getwriter (); string num1 = request. getparameter ("num1"); string num2 = request. getparameter ("num2"); system. Out. println ("num1 =>" + num1); system. Out. println ("num2 =>" + num2); If (num1! = NULL & num2! = NULL) {out. print (integer. parseint (num1) + integer. parseint (num2);} else {out. print ("invalide");} Out. flush (); out. close ();} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {This. doget (request, response);} public void Init () throws servletexception {// put your code here }}