Comprehensive application of jquery and Ajax (2)

Source: Internet
Author: User

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 }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.