Partial page refresh using Ajax

Source: Internet
Author: User
Two files, one HTML file, and one Servlet

The HTML code is:

<HTML>
<SCRIPT>
VaR XMLHTTP;

Window. onload = function (){
// Alert ("111 ");
Dostart ();
}

Function createxmlhttprequest (){
// Alert ("222 ");
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}

Function dostart (){
// Alert ("dostart ");
Createxmlhttprequest ();
VaR url = "dyupdate. YY ";

XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = startcallback;
XMLHTTP. Send (null );
}

Function startcallback (){
// Alert ("startcallback ");
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 ){
// Alert ("normal ");
SetTimeout ("pollserver ()", 5000 );
Refreshtime ();
}
Else {
Alert (XMLHTTP. status );
}
}}


Function pollserver (){
// Alert ("pollserver ");
Createxmlhttprequest ();
VaR url = "dyupdate. YY"
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = pollcallback;
XMLHTTP. Send (null );
}
Function pollserver2 (OB ){
// Alert (OB );
Createxmlhttprequest ();
VaR url = "dyupdate. yy & task =" + OB;
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = pollcallback2;
XMLHTTP. Send (null );
}
Function pollcallback2 (){}

Function pollcallback (){
// Alert ("pollcallback ");
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 ){
// Alert ("11 ");

VaR message = XMLHTTP. responsexml. getelementsbytagname ("message") [0]. firstchild. Data;
Alert (Message );
If (message! = ""){
VaR new_row = createrow (Message );
VaR table = Document. getelementbyid ("dyupdatearea ");
VaR table_body = table. getelementsbytagname ("tbody"). Item (0 );

VaR first_row = table_body.getelementsbytagname ("TR"). Item (1 );
Table_body.insertbefore (new_row, first_row );
SetTimeout ("pollserver ()", 5000 );
Refreshtime ();
}
Else {
Dostart ();
}}
Else {
Alert (XMLHTTP. status );
}
}
}

Function createrow (Message ){
// Alert ("createrow ");
VaR ROW = Document. createelement ("TR ");
VaR cell = Document. createelement ("TD ");
VaR cell_data = Document. createtextnode (Message );

Cell. appendchild (cell_data );
Row. appendchild (cell );
Return row;
}

Function refreshtime (){
// Alert ("refreshtime ");
VaR time_span = Document. getelementbyid ("time ");
VaR time_var = time_span.innerhtml;
VaR int_val = parseint (time_var );
VaR new_int_val = int_val-1;
 
If (new_int_val>-1 ){
SetTimeout ("refreshtime ()", 1000 );
Time_span.innerhtml = new_int_val;
} Else {
Time_span.innerhtml = 5;
}
}
</SCRIPT>
<Body>
<H1> Use Ajax <Input type = "button" value = "LOAD" id = "go" onclick = "dostart ();"/>
<P>
The page will be refreshed after <span id = "time"> 5 </span> seconds
</P>
<Table id = "dyupdatearea" align = "Left" border = "1">
<Tbody>
<Tr id = "row0">
<TD> </TD>
</Tr>
</Tbody>

<Input type = "text" id = "55" name = "text"/>
<Input type = "button" value = "Submit" onclick = "pollserver2 (document. getelementbyid ('55'). Value)"/>

</Table>
</Body>
</Html>

Servlet code:

Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. util. List;
Import java. util. Collections list;

Import javax. servlet. servletexception;
Import javax. servlet. http. Cookie;
Import javax. servlet. http. httpservlet;
Import javax. servlet. requestdispatcher;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Public class Ajax extends httpservlet {
Int counter;
Private Static writable list = new writable list ();
Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
// Response. SETC ("UTF-8 ");
If (list. Size ()> 0)
{
System. Out. println (list. getfirst (). tostring ());
}
System. Out. println ("executed ");
// Int NO = 1;

String res = "";
String task = request. getparameter ("task ");
If (task! = NULL &&! "". Equals (task ))
List. Add (task );
String message = "12 ";
// String Reno = "<count> 0 </count> ";


// If (task. Equals ("reset ")){
// Counter = 1;
//} Else {
// Switch (Counter ){
// Case 1: Message = "1"; break;
// Case 2: Message = "2"; break;
// Case 3: Message = "3"; break;
// Case 4: Message = "4"; break;
// Case 5: Message = "5"; break;
// Default: Message = "done ";
//}
// Counter ++;
//}

If (list. Size ()> 0 ){
Message = List. removelast (). tostring ();
}
Res = "<message>" + message + "</message> ";
Printwriter out = response. getwriter ();

Response. setcontenttype ("text/XML; charset = UTF-8 ");

Response. setheader ("cache-control", "No-Cache ");
Out. println ("<response> ");
Out. println (RES );
Out. println ("</response> ");
Out. Close ();
}

// Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
// String text = (string) request. getparameter ("text ");
// Response. setcontenttype ("text/XML; charset = UTF-8 ");
// If (text! = NULL)
//{
// Queue. Add (text );
//}
/// Requestdispatcher RD = This. getservletcontext (). getrequestdispatcher ("/ajax.html ");
/// RD. Forward (request, response );
//}
}

 

Related Article

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.