jsp+servlet+ajax网页版聊天室

//jsp代码

xmlHttp.onreadystatechange = function()
 { 
     // 得到服务器传回来的数据,放到table中
     
     var str = xmlHttp.responseText;     
     var jsonObj = eval(str);   
     var stU;  
     for(var i = 0;i<jsonObj.length;i++){
     
         if(jsonObj[i].chatMatter+jsonObj[i].chatDate=="undefined")
         {
              stU+="";
         }
        
      stU+="  "+jsonObj[i].chatUserName+"["+ jsonObj[i].chatDate+"]\r\n     "+jsonObj[i].chatMatter+"\r\n---------------------------------------------------\r\n";
     }
     var sub=stU.substring(9,stU.length);
     document.getElementById("sendContent").value ="---------------------------------------------------\r\n"+sub;
 }
    xmlHttp.setRequestHeader("If-Modified-Since","0");
    xmlHttp.send(null);

//servlet

ArrayList<Chat> arrayChat = new ArrayList<Chat>();
   arrayChat = chatDao.setChat(1, chatRoomId);
   StringBuffer sub = new StringBuffer("{");
   sub.append("[");
   int i = 0;
   for (Chat c : arrayChat) {
    sub.append("{");
    sub.append("chatMatter:\"" + c.getChatMatter() + "\",");
    sub.append("chatUserName:\"" + c.getChatUserName() + "\",");
    sub.append("chatDate:\"" + new SringUtils().subStringDateJin(c.getChatDate()) + "\"");
    i++;
    if (i < arrayChat.size()) {
     sub.append("},");
    } else {
     sub.append("}");
    }
   }
   sub.append("]}");
   System.out.println(sub.toString());
   PrintWriter writer = response.getWriter();
   writer.print(sub.toString());
   writer.flush();

</div>

相关推荐