Introduction
As we all know, we can use post or get to get the data of form forms. How can we directly obtain the data on the page without refreshing the submission? This requires the help of the XMLHTTP protocol. XMLHTTP is part of xmldom technology.
The following code is a simple example. We use XMLHTTP technology to implement simple user login.
Start
1. Simple logon pageLogin. jsp
<SCRIPT> Function toserver (){ VaR xml = "<root>" + "<Name>" + document. All ('name'). Value + "</Name>" + "<PWD>" + document. All ('pwd'). Value + "</pwd>" + "</Root> "; // VaR xmlsender = new activexobject ("Microsoft. XMLHTTP "); Xmlsender. Open ("Post", 'Do _ login. jsp ', false ); Xmlsender. Send (XML )); Alert (xmlsender. responsetext); // The result returned by the background can be processed. } </SCRIPT> Name: <input type = "text" id = "name"/> <br> Password: <input type = "text" id = "PWD"/> <br> <Input type = "button" value = "login" onclick = "toserver ()"> |
2. logon processing page in the backgroundDo_login.jsp
<% // Read the XMLHTTP stream Java. Io. bufferedreader BR = request. getreader (); String STR = ""; While (STR! = NULL ){ STR = Br. Readline (); Process(STR); // Parse XML in any language for Business Processing } // Return information Javax. servlet. servletoutputstream SOS = response. getoutputstream (); SOS. Print ("Login success "); SOS. Close (); %> |
3. The result is as follows:
Summary
Unlike the traditional "Submit-send-re-draw" web system, the basic operating structure of the system is different. We can use XMLHTTP to implement direct interaction between the client and the server, greatly improve user experience.
Exam Materials
XMLHTTP method:
Open bstrmethod, bstrurl, varasync, bstruser, bstrpassword
Bstrmethod: data transmission method, that is, get or post.
Bstrurl: the URL of the Service webpage.
Varasync: whether to run the command synchronously. The default value is true, that is, synchronous execution, but can only be performed in the Dom.
In applications, set it to false, that is, asynchronous execution.
Bstruser: user name, which can be omitted.
Bstrpassword: user password, which can be omitted.
Send varbody
Varbody: instruction set. It can be XML data, a string, a stream, or an unsigned integer array. It can also be omitted, so that the command can be substituted by the URL parameter of the open method.
SetRequestHeader bstrheader, bstrvalue
Bstrheader: HTTP Header)
Bstrvalue: HTTP header value
If the open method is defined as post, you can define form upload:
XMLHTTP. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded"
XMLHTTP attributes:
Onreadystatechange: obtains the event handle of the returned result in synchronous execution mode. It can only be called in the Dom.
Responsebody: returns an unsigned integer array.
Responsestream: The returned result is an istream stream.
Responsetext: returns a string.
Responsexml: The returned result is in XML format.
About the author