Ajax instance
Project Structure:
The content of each file is as follows:
Ajax. js:
Var xhr;
Function createXHR (){
Var xhr;
Try {
Xhr = new ActiveXObject ("Msxml2.XMLHTTP ");
Return xhr;
} Catch (e ){
Try {
Xhr = new ActiveXObject ("Microsoft. XMLHTTP ");
Return xhr;
} Catch (ee ){
Xhr = false;
}
}
If (! Xhr & typeof XMLHttpRequest! = 'Undefined '){
Xhr = new XMLHttpRequest ();
Return xhr;
}
Return xhr;
}
AjaxServlet01.java:
Package com. ajax. hw. web;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
/**
* Servlet implementation class AjaxServlet01
*/
Public class AjaxServlet01 extends HttpServlet
{
Private static final long serialVersionUID = 1L;
Public AjaxServlet01 ()
{
Super ();
}
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System. out. println ("request! ");
Response. setContentType ("text/html; charset = UTF-8 ");
PrintWriter out = response. getWriter ();
Out. print ("servlet Information! ");
}
Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException
{
DoGet (request, response );
}
}
Test01.jsp:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
<Script type = "text/javascript" src = "js/Ajax. js"> </script>
<Script type = "text/javascript">
Var xhr = createXHR ();
Xhr. onreadystatechange = onResponse;
Xhr. open ("GET", "AjaxServlet01", true );
Xhr. send (null );
Function onResponse2 (){
// Alert (xhr );
}
Function onResponse (){
Var obj = xhr;
If (obj. readyState = 0 ){
Document. getElementById ("copy"). innerHTML = "Sending Request ...";
}
If (obj. readyState = 1 ){
Document. getElementById ("copy"). innerHTML = "Loading Response ...";
}
If (obj. readyState = 2 ){
Document. getElementById ("copy"). innerHTML = "Response Loaded ...";
}
If (obj. readyState = 3 ){
Document. getElementById ("copy"). innerHTML = "Response Ready ...";
}
If (obj. readyState = 4 ){
If (obj. status = 200 ){
Var response = obj. responseText;
Alert ("obtained value:" + response );
Alert ("123 ");
Return true;
} Else if (obj. status = 404 ){
// Add a custom message or redirect the user to another page
Document. getElementById ("copy"). innerHTML = "File not found ";
} Else {
Document. getElementById ("copy"). innerHTML = "There was a problem retrieving the XML .";
}
}
};
</Script>
</Head>
<Body>
<Span id = "copy">
</Span>
</Body>
</Html>
Web. xml:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5">
<Display-name> ajax00001 </display-name>
<Welcome-file-list>
<Welcome-file> index.html </welcome-file>
<Welcome-file> index.htm </welcome-file>
<Welcome-file> index. jsp </welcome-file>
<Welcome-file> default.html </welcome-file>
<Welcome-file> default.htm </welcome-file>
<Welcome-file> default. jsp </welcome-file>
</Welcome-file-list>
<Servlet>
<Description> </description>
<Display-name> AjaxServlet01 </display-name>
<Servlet-name> AjaxServlet01 </servlet-name>
<Servlet-class> com. ajax. hw. web. AjaxServlet01 </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> AjaxServlet01 </servlet-name>
<Url-pattern>/AjaxServlet01 </url-pattern>
</Servlet-mapping>
</Web-app>
Author "whuang"