Ajax Study Notes 01

Source: Internet
Author: User

Ajax Study Notes 01

Course:

Deploy the first Ajax Program

Learning steps:

 

1. Create a Java Web project -- ajax_01:

 

2. Create servlet, package name test, class name ajaxservlet, dopost () code:

 

  1. Request. setcharacterencoding ("UTF-8 ");
  2. Response. setcontenttype ("text/html; charset = UTF-8 ");
  3. Printwriter out = response. getwriter ();
  4. String S = request. getparameter ("username ");
  5. Out. Print ("returned result:" + S );

 

 

3. Modify the index. jsp page code:

 

  1. <Div id = "result"> </div>
  2. <Form ID = "form1" Action = "Servlet/ajaxservlet" method = "Post">
  3. <Input type = "text" id = "username" name = "username"/>
  4. <Input type = "button" id = "Submit" value = "Submit"
  5. Onclick = "verify ()"/>
  6. </Form>

 

 

4. Create the jslib package under webroot, put jquery. js in this package, create the verify. js file, and write the code:

 

  1. Function verify (){
  2. // 1. Obtain the content in the text box
  3. VaR jqueryobj = $ ("# username ");
  4. VaR username = jqueryobj. Val ();
  5. Alert (username );
  6. // 2. Send the data in the text box to the Servlet
  7. // Suspect that when the following code sends data to the servlet, garbled characters will be transmitted in Chinese.
  8. // If you press enter directly in the editing box, the servlet will be loaded directly, and the JS page will be loaded first after you click the button,
  9. // Then pass the obtained data to the servlet through this JS page
  10. $. Get ("Servlet/ajaxservlet? Username = "+ username, null, callback/* callback function */);
  11. }
  12. Function callback (data ){
  13. Alert ("the server data is back ");
  14. // 3. Accept the data returned by the server
  15. VaR resultobj = $ ("# result ");
  16. // 4. dynamically display the data returned by the server to the page
  17. Resultobj.html (data );
  18. }

5. Import JS Code in index. jsp:

 

  1. <SCRIPT type = "text/JavaScript" src = "jslib/verify. js"> </SCRIPT>
  2. <SCRIPT type = "text/JavaScript" src = "jslib/jquery. js"> </SCRIPT>

 

 

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.