Keywords: Example dialog box
Java code for JS File Content
- // Define the XMLHttp instance
- Var xmlHttp;
- Function createXMLHttpRequest (){
- // Start initializing the XMLHttpRequest object
- If (window. ActiveXObject ){
- // IE browser
- XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
- } Else if (window. XMLHttpRequest ){
- // Mozilla Browser
- XmlHttp = new XMLHttpRequest ();
- }
- If (xmlHttp ){
- // Set the request address
- XmlHttp. open ("GET", "message. do? Cmd = get ", true );
- XmlHttp. onreadystatechange = setState; // sets the callback function.
- XmlHttp. send (null );
- }
- }
- // Callback function
- Function setState (){
- If (xmlHttp. readyState = 4 ){
- If (xmlHttp. status = 200 ){
- ParseResults (xmlHttp. responseXML)
- }
- }
- }
- /* Process XML */
- Function parseResults (xmlDOM ){
- Try {
- Var root = xmlDOM.doc umentElement;
- Var info = root. getElementsByTagName ('message ');
- Var out = "";
- Var message = null;
- Var current = null;
- For (var I = 0; I <info. length; I ++ ){
- Message = info [I];
- Id = message. getElementsByTagName ("id") [0]. firstChild. data;
- Title = message. getElementsByTagName ("title") [0]. firstChild. data;
- Out = out + (I + 1) + ". "+" <a style = \ "font-size: 12px; color: #6D93C8; face: Tahoma \" title = 'content feed: \ n"
- + Title
- + "\ N time :"
- + Title
- + "'" + "Href = #>"
- + Title
- + "</A> <BR> ";
- }
- Popmsg ("<BR>" + out );
- } Catch (exception ){
- // Alert ("error! ");
- }
- }
- /* Prompt box in the lower right corner */
- Var oPopup = window. createPopup ();
- Var popTop = 50;
- Function popshow (){
- OPopup. show (screen. width-250, screen. height-popTop, 241,172 );
- }
- Function pophide (){
- OPopup. hide ();
- }
- Function popmsg (msgstr ){
- Var winstr = "<table valign = \" top \ "style = \" border: 1 solid #6D93C8 \ "width = \" 241 \ "height = \" 172 \ "border = \" 0 \ "cellpadding = \" 0 \ "cellspacing = \" 0 \ "> ";
- Winstr + = "<tr> <td bgcolor = \" # BCCCDF \ "onClick = \" parent. pophide (); \ "valign = \" top \ "align = \" center \ "height = \" 20 \ "style = \" font-size: 14px; color: #6D93C8; face: Tahoma; filter: Blur (add = 1, direction = 45, strength = 8) \ "> you have a new short message: </td> </tr> <td valign = \ "top \" align = \ "center \"> <table valign = \ "top \" width = \ "90% \" height = \ "110 \" border = \ "0 \" cellpadding = \ "0 \" cellspacing = \ "0 \"> ";
- Winstr + = "<tr> <td valign = \" top \ "style = \" font-size: 12px; color: #6D93C8; face: tahoma \ ">" + msgstr + "</td> </tr> </table> ";
- OPopup.doc ument. body. innerHTML = winstr;
- Popshow ();
- SetInterval ('window. focus () ', 0); // re-obtain the focus of IE
- }
- /* Reminder interval */
- Var secs = 5; // seconds for the first prompt
- Function checkServer (secs ){
- For (I = 1; I <= secs; I ++ ){
- Window. setTimeout ("update (" + I + ")", I * 1000 );
- }
- }
- Function update (num ){
- If (num = secs ){
- CreateXMLHttpRequest ();
- Secs = 1*60; // seconds
- For (I = 1; I <= secs; I ++ ){
- Window. setTimeout ("update (" + I + ")", I * 1000 );
- }
- }
- Else {
- Printnr = secs-num;
- }
- }
- CheckServer (secs );
// Define the XMLHttp instance var xmlHttp; function createXMLHttpRequest () {// start initializing the XMLHttpRequest object if (window. activeXObject) {// xml http = new ActiveXObject ("Microsoft. XMLHTTP ");} else if (window. XMLHttpRequest) {// Mozilla xmlHttp = new XMLHttpRequest ();} if (xmlHttp) {// set the request address xmlHttp. open ("GET", "message. do? Cmd = get ", true); xmlHttp. onreadystatechange = setState; // sets the callback function xmlHttp. send (null) ;}// callback function setState () {if (xmlHttp. readyState = 4) {if (xmlHttp. status = 200) {parseResults (xmlHttp. responseXML) }}/ * process XML */function parseResults (xmlDOM) {try {var root = xmlDOM.doc umentElement; var info = root. getElementsByTagName ('message'); var out = ""; var message = null; var current = null; for (var I = 0; I <inf O. length; I ++) {message = info [I]; id = message. getElementsByTagName ("id") [0]. firstChild. data; title = message. getElementsByTagName ("title") [0]. firstChild. data; out = out + (I + 1) + ". "+" <a style = \ "font-size: 12px; color: #6D93C8; face: Tahoma \" title = 'Content Abstract: \ n "+ title +" \ n time: "+ title +" '"+" href = #> "+ title +" </a> <BR> ";} popmsg ("<BR>" + out);} catch (exception) {// alert ("error! ") ;}}/* Prompt box at the bottom right corner */var oPopup = window. createPopup (); var popTop = 50; function popshow () {oPopup. show (screen. width-250, screen. height-popTop, 241,172);} function pophide () {oPopup. hide ();} function popmsg (msgstr) {var winstr = "<table valign = \" top \ "style = \" border: 1 solid #6D93C8 \ "width = \" 241 \ "height = \" 172 \ "border = \" 0 \ "cellpadding = \" 0 \ "cellspacing = \" 0 \ "> "; winstr + = "<tr> <td bgcolor = \" # BCCCDF \ "onClick = \" parent. pophide (); \ "valign = \" top \ "align = \" center \ "height = \" 20 \ "style = \" font-size: 14px; color: #6D93C8; face: Tahoma; filter: Blur (add = 1, direction = 45, strength = 8) \ "> you have a new short message: </td> </tr> <td valign = \ "top \" align = \ "center \"> <table valign = \ "top \" width = \ "90% \" height = \ "110 \" border = \ "0 \" cellpadding = \ "0 \" cellspacing = \ "0 \"> "; winstr + = "<tr> <td valign = \" top \ "style = \" font-size: 12px; color: #6D93C8; face: tahoma \ ">" + msgstr + "</td> </tr> </table>" Your opopup.doc ument. body. innerHTML = winstr; popshow (); setInterval ('window. focus () ', 0); // Let IE re-obtain the focus}/* prompt interval */var secs = 5; // the first time the system prompts the second function checkServer (secs) {for (I = 1; I <= secs; I ++) {window. setTimeout ("update (" + I + ")", I * 1000) ;}} function update (num) {if (num = secs) {createXMLHttpRequest (); secs = 1*60; // The prompt time seconds for (I = 1; I <= secs; I ++) {window. setTimeout ("update (" + I + ")", I * 1000) ;}} else {printnr = secs-num ;}} checkServer (secs );
Java code for the XML content of the request
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Messages>
- <Message>
- <Id> 001 </id>
- <Title> inform you of coming to the meeting today </title>
- </Message>
- </Messages>
<? Xml version = "1.0" encoding = "UTF-8"?> <Messages> <message> <id> 001 </id> <title> notify you to come to the meeting today </title> </message> </messages>
If XML is to be dynamically generated, use the following Java code:
- <% @ Page import = "java. util. List, com. sunflower. model. Message" contentType = "text/xml; charset = UTF-8" %>
- <%
- Response. setContentType ("text/xml ");
- Response. setHeader ("Cache-Control", "no-cache ");
- List list = (List) request. getAttribute ("messages ");
- Out. println ("<messages> ");
- If (list! = Null)
- For (int I = 0; I <list. size (); I ++ ){
- Message objM = (Message) list. get (I );
- Out. println ("<message> ");
- Out. println ("<id>" + objM. getId () + "</id> ");
- Out. println ("<title>" + objM. getTitle () + "</title> ");
- Out. println ("</message> ");
- }
- Out. println ("</messages> ");
- %>
<%@ page import="java.util.List,com.sunflower.model.Message" contentType="text/xml;charset=utf-8"%><%response.setContentType("text/xml");response.setHeader("Cache-Control", "no-cache");List list = (List)request.getAttribute("messages");out.println("<messages>");if(list!=null)for(int i=0;i<list.size();i++){Message objM = (Message)list.get(i);out.println("<message>");out.println("<id>"+objM.getId()+"</id>");out.println("<title>"+objM.getTitle()+"</title>");out.println("</message>");}out.println("</messages>"); %>
Java code for HTML file content
- <Html>
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
- <Title> A prompt is displayed in the lower right corner. </title>
- </Head>
- <Script type = "text/javascript" src = "ajax. js"> </script>
- <Body>
- <Input type = "button" onclick = "createXMLHttpRequest ()" value = "test request"/>
- <Div id = "contain"> </div>
- </Body>
- </Html>
Original post address: http://jinghua.javaeye.com/blog/114323