30 minutes to learn how to use Ajax:

Source: Internet
Author: User
Tags call back

The following example shows how to use the post method of Ajax. The post method of AJAX is usually used.

In this example, enter a user name to check whether the user name can be used.

<1>: Create an Ajax Tool class

(1): Create ajaxutils. js

(2): the code is as follows:

VaR Ajax = {createxhr: function () {// create an xhr object if (window. XMLHttpRequest) {// for VAR xhr = new XMLHttpRequest (); Return xhr;} else if (window. activexobject) {// var xhr = new activexobject ("Microsoft. XMLHTTP "); Return xhr ;}, sendrequest: function (method, URL, Data, callback) {// callback indicates the function name var xhr = This. createxhr (); xhr. open (method, URL, true); If ("get" = method. touppercase () {xhr . Send (null);} else if ("Post" = method. touppercase () {xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded"); xhr. send (data);} xhr. onreadystatechange = function () {If (xhr. readystate = 4 & xhr. status = 200) {// callback (xhr); you can call back ({text: xhr. responsetext, XML: xhr. responsexml}); // You can also. This is the best case, object-oriented }}}};

<2>: Create JSP and introduce the ajaxutils. js tool class in JSP. the JSP code is as follows:

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 
 

<3>: Configure web. xml under the WEB-INF

 <servlet>  <servlet-name>ajaxPostServlet</servlet-name>  <servlet-class>com.web.AjaxPostServlet</servlet-class>  </servlet>  <servlet-mapping>  <servlet-name>ajaxPostServlet</servlet-name>  <url-pattern>/servlet/AjaxPostServlet</url-pattern>  </servlet-mapping>

<4>: Create a servlet, such as ajaxpostservlet. The Code is as follows:

Package COM. web; import Java. io. ioexception; import Java. io. printwriter; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public class ajaxpostservlet extends httpservlet {@ overrideprotected void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); response. setcontenttype ("text/html; charset = UTF-8"); response. setheader ("cache-control", "No-Cache"); printwriter out = response. getwriter (); string username = request. getparameter ("username"); string Password = request. getparameter ("password"); If ("zhangsan ". equals (username) {// here is out. print is not out. printlnout. print ("unavailable"); // out. the content in print () will be output to the callback function} else {out. print ("available");} Out. flush (); out. close ();}}

 

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.