JSP + Ajax auto-Refresh instance

Source: Internet
Author: User

I. jsp file: auto. jsp
<% @ Page contenttype = "text/html; charset = gb2312" %>

<%
// Set the format and Character Set of output information
Response. setcontenttype ("text/XML; charset = UTF-8 ");
Response. setheader ("cache-control", "No-Cache ");
Out. println ("<response> ");

For (INT I = 0; I <2; I ++ ){
Out. println ("<Name>" + (INT) (math. Random () * 10) + "notebook </Name> ");
Out. println ("<count>" + (INT) (math. Random () * 100) + "</count> ");
}
Out. println ("</response> ");
Out. Close ();

%>

 

 
Ii. html file: autorefresh.html
<Head>
<Meta http-equiv = Content-Type content = "text/html; charset = gb2312">
</Head>
<Script language = "JavaScript">
VaR xmlhttpreq;
// Create an XMLHTTPRequest object
Function createxmlhttprequest (){
If (window. XMLHttpRequest) {// Mozilla Browser
Xmlhttpreq = new XMLHttpRequest ();
}
Else if (window. activexobject) {// IE browser
Try {
Xmlhttpreq = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Xmlhttpreq = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
}
// Send the request Function
Function sendrequest (){
Createxmlhttprequest ();
VaR url = "auto. jsp ";
Xmlhttpreq. Open ("get", URL, true );
Xmlhttpreq. onreadystatechange = processresponse; // specify the response function.
Xmlhttpreq. Send (null); // send the request
}
// Function for processing returned information
Function processresponse (){
If (xmlhttpreq. readystate = 4) {// judge the object status
If (xmlhttpreq. Status = 200) {// The information has been returned successfully. Start to process the information.
Displayhot ();
SetTimeout ("sendrequest ()", 1000 );
} Else {// The page is abnormal.
Window. Alert ("the page you requested has an exception. ");
}
}
}
Function displayhot (){
VaR name = xmlhttpreq. responsexml. getelementsbytagname ("name") [0]. firstchild. nodevalue;
VaR COUNT = xmlhttpreq. responsexml. getelementsbytagname ("count") [0]. firstchild. nodevalue;
Document. getelementbyid ("product"). innerhtml = Name;
Document. getelementbyid ("count"). innerhtml = count;
}

</SCRIPT>

<Body onload = sendrequest ()>
<Table Style = "border-collapse: collapse" bordercolor = #111111
Cellspacing = 0 cellpadding = 0 width = 200 bgcolor = # f5efe7 border = 0>
<Tr>
<TD align = Middle Height = 4 colspan = "2"> Src = "images/promo_list_top.gif" width = "100%"
Border = 0>
</TD>
</Tr>
<Tr>
<TD align = middle bgcolor = # dbc2b0
Height = 19 colspan = "2"> <B> Hot notebook </B>
</TD>
</Tr>
<Tr>
<TD Height = "20">
Model:
</TD>
<TD Height = "20" id = "product">
</TD>
</Tr>
<Tr>
<TD Height = "20">
Sales quantity:
</TD>
<TD Height = "20" id = "count">
</TD>
</Tr>
</Body>
</Table>

 

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.