1. Web. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<Display-name/>
<Servlet>
<Servlet-Name> action </servlet-Name>
<Servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class>
<Init-param>
<Param-Name> config </param-Name>
<Param-value>/WEB-INF/struts-config.xml </param-value>
</Init-param>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> 3 </param-value>
</Init-param>
<Init-param>
<Param-Name> detail </param-Name>
<Param-value> 3 </param-value>
</Init-param>
<Load-on-startup> 0 </load-on-startup> <! -- Instantiate the service as soon as it is started -->
</Servlet>
<Servlet-mapping>
<Servlet-Name> action </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>
2. struts-config.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.3 //" http://struts.apache.org/dtds/struts-config_1_3.dtd ">
<Struts-config>
<Form-beans>
<! -- Let struts know the form and provide it to action. Name is the reference name of form, and type is the full name of the corresponding form class. -->
<Form-bean name = "messageform" type = "com. hollycrm. Form. messageform">
</Form-bean>
</Form-beans>
<Global-exceptions/>
<Global-forwards/>
<Action-mappings>
<Action Path = "/message" type = "com. hollycrm. Action. messageaction" name = "messgeform">
<Forward name = "success" Path = "/index. jsp"> </forward>
</Action>
</Action-mappings>
<Message-resources parameter = "com. yourcompany. Struts. applicationresources"/>
</Struts-config>
3. index. jsp
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb18030" %>
<% @ Taglib uri = "http://struts.apache.org/tags-html" prefix = "html" %>
<% String Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<SCRIPT src = "JS/create. js" Language = "JavaScript">
</SCRIPT>
</Head>
<Body>
<Font size = "2"> </font>
Name: <input type = "text" id = "name" onblur = "sayhello ()"> <br/>
Value returned by Ajax: <span id = 'hello'> </span> <br>
</Body>
</Html>
4. Create. js
// Create the XMLHTTPRequest object function createxmlhttprequest (){
// Determine whether the browser supports the XMLHTTPRequest object through the local JavaScript Method
If (window. XMLHttpRequest) {// Mozilla Browser
// Method for creating the XMLHTTPRequest object in other browsers except IE
Xmlhttpreq = new XMLHttpRequest ();
} Else {// IE browser
// Determine whether the browser supports ActiveX. in IE, the ActiveX component supports XMLHttpRequest objects.
If (window. activexobject) {// in IE browsers of different versions, the supported methods for XMLHttpRequest objects are somewhat different.
// Use the following code to adapt to different versions of IE browsers:
Try {
Xmlhttpreq = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Xmlhttpreq = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}}}}}
// Process the Server Response Request, analyze and process the information returned by the server, and display the analysis result in the specified position on the page
Function handleresponse (){
// Determine the object status. The readystate attribute describes the Request status. The following five values can be taken down: 0 (not initialized), 1 (loading), 2 (loaded ), 3 (in interaction), 4 (completed)
If (xmlhttpreq. readystate = 4) {// The information has been returned successfully. Start to process the information.
// The status attribute describes the status code of the server, which can be 200,404.
If (xmlhttpreq. Status = 200 ){
VaR out = "";
// Responsexml is the response returned by the server in XML format.
VaR res = xmlhttpreq. responsexml;
VaR response = res. getelementsbytagname ("response") [0]. firstchild. nodevalue;
Document. getelementbyid ("hello"). innerhtml = response;
// Document. getelementsbyname ("hello"). innerhtml = response;
}}}
// Send client requests
Function sendrequest (URL ){
Createxmlhttprequest ();
// The open (string method, string URL, Boolean asynch, string name, string password) method is used to call the server.
// This method has five parameters. The first two parameters are required. The method can be get or post, and the URL is the location of the server resource to be accessed, aysnch refers to asynchronous access to the server or synchronous access. By default, it is asynchronous access, which is exactly the advantage of Ajax. The last two parameters can be used to access server resources with the specified user name and password.
Xmlhttpreq. Open ("get", URL, true); // specify the Response Function
// Onreadystatechange attribute. This event processor is called for every state change. This attribute can be used to listen for state changes and provide corresponding processing methods.
Xmlhttpreq. onreadystatechange = handleresponse;
// Send the request. The send (content) method sends the request to the server. The parameters in this method are sent as the content in the request to the server.
Xmlhttpreq. Send (null );
}
// Start calling the Ajax Function
Function sayhello (){
VaR name = Document. getelementbyid ("name"). value;
// Alert (name );
// Send the request
Sendrequest ("message. do? Name = "+ name );
}
5. messageaction. Java
Public class messageaction extends action {
Public actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {
// Messageform = (messageform) form;
// Set the file generation type and encoding method
Response. setcontenttype ("text/XML; charset = UTF-8 ");
Response. setheader ("cache-control", "No-Cache ");
Printwriter returnvalue = response. getwriter ();
String output = ""; // process the received parameters and generate the XML document for the response
// System. Out. println (messageform. getname ());
// System. Out. println (request. getparameter ("name "));
// If (messageform. getname (). Equals ("15979089546 ")){
If (request. getparameter ("name"). Equals ("15979089546 ")){
Output = "<response> Wen shengming </response> ";
} Else {
Output = "<response> this user does not exist </response> ";
}
Returnvalue. Print (output );
Returnvalue. Close ();
Return Mapping. findforward ("sucess ");
}}
6. messageform. Java
Public class messageform extends actionform {
Private string name = NULL;
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}}