Ajax instance 01

Source: Internet
Author: User

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"

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.