The use of Ajax in Java

Source: Internet
Author: User

Ajax
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<script type= "Text/javascript" >
/* Standard AJAX Template */
Use false as a decision condition, which means that the XMLHttpRequest object has not yet been created
var Http_request=false;
To create a XMLHttpRequest object method
function Send_request () {
Http_request=false;
Firefox
if (window. XMLHttpRequest) {
Http_request=new XMLHttpRequest ();
if (Http_request.overridemimetype) {
Http_request.overridemimetype (' Text/xml ');
}
}else if (window. ActiveXObject) {//ie
try{
Http_request=new ActiveXObject ("msxml2.xmlhttp");
}catch (e) {
try{
Http_request=new ActiveXObject ("Microsoft.XMLHTTP");
}catch (E2) {}
}
}

if (!http_request) {//Check if request is still false (not false if all goes well)
Window.alert ("Err Create xmlhttprequest!");
}

}

Interacting with the server
function Sendreg (URL)
{
Creating an AJAX engine
Send_request ();
Set callback function
Callback functions are called automatically once the server has completed a response
Http_request.onreadystatechange = Requestreg;
Connect to Server
1. Submission Method (Get/post)
2. Submission Path (URL)
3. Whether asynchronous
Http_request.open ("Get", url,true);
Using the Get method does not cache
Http_request.setrequestheader ("If-modified-since", "0");
Submit Request
Get null
Http_request.send (NULL);
}

//Event function
Function test ()
{
//Get Data for text box
var text = document.getElementById ("text"). Value;
//get
/ /define the current Access server URL
var url = "reg?text=" + text;
//decode the string using UTF-8 encoding
URL = encodeuri (URL);
//interact with server
Sendre g (URL);
}
//define callback function
Function Requestreg ()
{
//OK State
if (http_request.readystate = = 4)
{
//Judgment response status br> if (http_request.status = =)
{
//Get data for server response
var res = http_request.responsetext;
// Note that this is done by using OUT.PRINTLN ("* * * *") in the servlet, and you can also send the data in XML format, which is var res = http_request.responsexml; You can then use the Res.getelementsbytagname and other DOM parsing methods to parse, this can be found on the Internet related information

//alert (RES);
if (res = = "true")
{
document.getElementById ("result"). InnerHTML = "<font color= ' red ' > This username has already been registered < /FONT> ";
document.getElementById ("submit"). Disabled = true;
}
Else
{
document.getElementById ("result"). InnerHTML = "<font color= ' Blue ' > This username is not registered </font > ";
document.getElementById ("submit"). Disabled = false;
}
}
}
}

</script>

<body style= "Test-align:center" onload= "Add ()" >
<table align= "center" cellspacing= "0" width= "$" border= "0" >
<tr>
<TD width= ">" username </td>
<TD width= "><input" type= "text" id= "text" onblur= "Test ()"/></td>
<TD width= "$" id= "result" ></td>
</tr>
<TR align= "center" >
<TD colspan= "2" ><input type= "button" value= "register" id= "submit" disabled= "disabled"/></td>
<td></td>
</tr>
</table>
</body>

The use of Ajax in Java

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.