Ajax asynchronous transmission is widely used. Let's take web development as an example. When a user registers, how can he determine whether the user exists immediately and display a prompt after the input box? This uses asynchronous transmission ~~
It allows you to retrieve the data you want in the background without leaving the page and display it on the current page !! Okay. Let's take a look at the example below.
The following is an Ajax verification code.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// ① Set the variable
Var xmlHttp;
Var flag = false;
Var package_name;
// ② Create an XMLHttp object
Function createXMLHttp (){
If (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();
} Else {
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
}
// ③
Function getPackage_name (package_name ){
// Create an XMLHttpRequest object
CreateXMLHttp ();
Document. getElementById ("msg"). innerHTML = package_name;
// Create an HTTP request and send package_name to AppInfoServlet in post Mode
XmlHttp. open ("post", "AppInfoOneServlet? Package_name = "+ package_name );
// Call the callback function to generate a visual page response
XmlHttp. onreadystatechange = checkCallback;
// Send the request
XmlHttp. send (null );
// Friendly prompt
Document. getElementById ("msg"). innerHTML = "getting information. Please wait ...";
}
// ④ Callback function
Function checkCallback (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Var text = xmlHttp. responseText;
Var t = text. split ('|'); // when multiple values are obtained and separated with "|"
Document. getElementById ("msg "). innerHTML = t [0] + "<br>" + "<div id = 'Container'>" + "<div id = 'content'>" + "<div id = 'slider'> "+" <ul> "+" <li> "+" "+" </li> "+" <li> "+" "+" </li> "+" </ul> "+" </div> "+" // The obtained data is displayed in the id "msg", for example, <span id = "msg"> </span>
}
}
}
</Script>
In this way, js functions are called on the jsp page.
Copy codeThe Code is as follows:
<A href = "# bottom" onclick = "getPackage_name ('<% = ainfo. getPackage_name () %>')"> Get </a>
The data obtained by Ajax is displayed in the following tag:
Copy codeThe Code is as follows:
<Span id = "msg"> </span>
Okay. Let's take a look at the servlet code:
Copy codeThe Code is as follows:
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Request. setCharacterEncoding ("UTF-8 ");
Response. setCharacterEncoding ("UTF-8"); // It must be before the out; otherwise, the transmitted man is garbled.
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_export path () + "|" + ainfo. getScreen_2_path () + "|" + ainfo. getDownload_path ());
}
The above is clear. Of course, it is the same as verifying whether a user exists. Pay attention to the garbled Characters During Chinese Character transmission ~~~~~~