Struts+Ajax实现定时刷新Table内容,实现在线人员显示收藏

在开发在线收费系统时,因为考及时显示所有在线用户,所以采用了AJAX来实现,这是我的一点体会

Ajax代码如下:



<script language="JavaScript" type="">
    var XMLHttpReq = false;
    //创建XMLHttpRequest对象
    function createXMLHttpRequest()...{
        if(window.XMLHttpRequest)...{                //Mozilla浏览器
            XMLHttpReq = new XMLHttpRequest();
        }
        else if(window.ActiveXObject)...{          //IE浏览器
            try...{
                XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e)...{
                try...{
                    XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                }catch(e)...{}
            }
        }
    }
    //发送请求函数
    function sendRequest()...{
        var url="handlerList.do"
        createXMLHttpRequest(); //调用创建对象方法
        XMLHttpReq.open("post",url,true);
        XMLHttpReq.onreadystatechange = processResponse;//指定响应函数 调用
        XMLHttpReq.setRequestHeader("If-Modified-Since","0");
        XMLHttpReq.send(null);//发送请求
    }
    //处理返回信息函数
    function processResponse()...{
        if(XMLHttpReq.readyState==4)...{//代表服务器已经传回所有的信息,可以处理了
            if(XMLHttpReq.status==200)...{   //信息已经成功返回,开始处理信息
                updateTable();
                setInterval("sendRequest()",10000);
            }else   //页面不正常
                window.alert("您所请求的页面有异常。");
        }
       
    }

                     //更新table的内容
    function updateTable()...{
        clearTable();
        var results=XMLHttpReq.responseXML.getElementsByTagName("handler");
        for(var i=0;i<results.length;i++)...{
                                       var name=results[i].childNodes[0].firstChild.nodeValue;
                                       addTableRow(name);
                                          }
    }
                     //向table的行添加单元格
    function addTableRow(name)...{
        var table=document.getElementById("handlerList");
        var row = table.insertRow(1);
        var cell = row.insertCell(0);
        cell.align="center";
        cell.innerHTML="<strong>"+name+"</strong>";
    }
                     //清除表格内容
    function clearTable()...{
        var table=document.getElementById("handlerList");
        for(var i=table.rows.length-1;i>0;i--)
        ...{
            table.deleteRow(i);
        }
    }
</script>
下面是JSP的内容



<body onload="sendRequest()">
  <table width = "124" height="100%" border="0" cellspacing="0" background="../image/bak.jpg">
  <tr>
    <td height="39" align="center" valign="middle"><p align="center"><html:link forward="main"><strong>业务受理</strong></html:link></p>
    </td>
  </tr>
  <tr>
    <td height="36" align="center" valign="middle"><html:link forward="payMoney"><strong>业务处理</strong></html:link></td>
  </tr>
  <tr>
    <td height="35" align="center" valign="middle"><html:link forward="query"><strong>业务查询</strong></html:link></td>
  </tr>
  <tr>
  <td height="35" align="center" valign="middle"><html:link forward="update"><strong>&#36164;&#26009;&#20462;&#25913;</strong></html:link></td>
  </tr>
  <tr>
  <td height="42" align="center" valign="middle"><strong><html:link href="admin.jsp">&#31649;&#29702;</html:link></strong></td>
  </tr>
  <tr>
  <td height="34" align="center" valign="middle"><strong><br>
    &#36864;&#20986;</strong>
  </td>
  </tr>
  <tr>
  <td height="21" align="center" valign="middle">&nbsp;
  </td>
  </tr>
      <tr>
          <td align="center" valign="middle">
              <table id="handlerList" width="100%" height="20"  border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td align="center"><strong>在线人员:</strong></td>
              </tr>
            </table>
        </td>
      </tr>
</table>
</body>
handlerListAction内容如下



public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) ...{
        try ...{
            request.setCharacterEncoding("GBK");
        } catch (UnsupportedEncodingException e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.setContentType("text/xml;charset=GBK");
        response.setHeader("Cache-Control","no-cache");
       
        ServletContext context=getServlet().getServletContext();
        List list=(List)context.getAttribute("onlineHandler");
       
        PrintWriter out = null;
        try ...{
            out = response.getWriter();
        } catch (IOException e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String str="";
        str="<?xml version="1.0" encoding="GBK"?>";
        str+="<handlers>";
        for(int i=0;i<list.size();i++)
        ...{
            str+="<handler>";
            Handler handler=(Handler)list.get(i);
            str+="<name>"+handler.getHandlerName()+"</name>";
            str+="</handler>";
        }
        str+="</handlers>";
        out.println(str);
        out.close();
        return null;
    }


为了实现对在线人员的统计

我在登陆的时候将登陆人员放入了ServletContext中,然后使用HttpSessionListenter来监视登陆用户情况



public class SessionListenter implements HttpSessionListener ...{

    public SessionListenter()
    ...{}
    public void sessionCreated(HttpSessionEvent arg0) ...{
        // TODO Auto-generated method stub
        System.out.println("Session监听启动了");
        arg0.getSession().setMaxInactiveInterval(1*60);//设置Session的过期时间为1分钟
    }

    public void sessionDestroyed(HttpSessionEvent arg0) ...{
        System.out.println("session释放启动了");
       
        HttpSession session = arg0.getSession();
        ServletContext ctx = session.getServletContext();
        List list = (List)ctx.getAttribute("onlineHandler");
        list.remove(session.getAttribute("handler"));
        System.out.println("List的个数为:"+list.size());
    }

你可能感兴趣的:(Ajax,struts,浏览器,cache,IE)