Ajax asynchronous transmission is very broad, take the web development, when the user registration, when the user just lost, how to immediately determine the existence of users, and in the input box after the display prompts? This is the asynchronous transfer ~ ~
it allows you to not leave the page in case you get the data you want in the background and display it on the current page!! OK, let's look at an example
The following is an AJAX validation code
Copy Code code as follows:
<script type= "Text/javascript" >
//① settings variable
var xmlHttp;
var flag = false;
Var package_name; The
//② creates a XMLHTTP object
function Createxmlhttp () {
if window. XMLHttpRequest) {
XmlHttp = new XMLHttpRequest ();
} else {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
}
//③
Function Getpackage_name (package_name) {
//Create XMLHttpRequest Object
Createxmlhttp ();
document.getElementById ("msg"). Innerhtml=package_name;
//Create an HTTP request to post the package_name to Appinfoservlet
Xmlhttp.open ("Post", "Appinfooneservlet?package_name=" +package_name);
//Call callback function to generate a visual page response
Xmlhttp.onreadystatechange =checkcallback;
//Send request
Xmlhttp.send (NULL);
//Friendly tip
document.getElementById ("msg"). InnerHTML = "Getting information, please wait patiently ...";
}
//④ callback functionBr>function Checkcallback () {
if (xmlhttp.readystate = 4) {
if (xmlhttp.status = =) {
var tex t = Xmlhttp.responsetext;
var t=text.split (' | '); /When you get more than one value and use the "|"
document.getElementById ("MSG") when split. InnerHTML =t[0]+ "<br>" + "<div id= ' container ' >" + "<div id=" Content ' > ' + "<div id= ' slider ' >" + "<ul>" + "<li>" + " "+" </li> "+" <li> "+" ' + ' </li> ' + ' </ul> "+" </div> "+" //Gets data is displayed in the tag with id "MSG", for example: <span id= "msg" ></span>
}
}
}
</script>
/div>
in the JSP page is the way to call the JS function
Copy Code code as follows:
<a href= "#bottom" onclick= "Getpackage_name (' <%=ainfo.getpackage_name ()%> ')" > Get </a>
Ajax-acquired data will be displayed in the following markup
Copy Code code as follows:
OK, let's take a look at the code in the servlet:
Copy Code code as follows:
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Request.setcharacterencoding ("Utf-8");
Response.setcharacterencoding ("Utf-8");/must be out before, otherwise transmission of the man for garbled AH
PrintWriter out = Response.getwriter ();
String package_name=request.getparameter ("Package_name");
Appdaoimpl adi=new Appdaoimpl ();
AppInfo ainfo=new AppInfo ();
Ainfo=adi.getoneappinfo (package_name);
Out.print (ainfo.getdescription () + "|" +ainfo.getscreen_1_path () + "|" +ainfo.getscreen_2_path () + "|" +ainfo.getdownload_path ());
}
above already very clear, of course, verify the existence of the user, the truth and this is exactly the same, which must pay attention to the problem of Chinese characters in the transmission of garbled ~~~~~~