java 类某值改变,jsp页面也更新。。。

肖宁 发布于 2011/05/16 15:41
阅读 622
收藏 1

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="org.jivesoftware.smack.*"%>
<%@ page import="org.jivesoftware.smack.packet.*"%>
<%String sendMsg = request.getParameter("msg"); %>
<%!
   class Login{
    private String state="";
    private String mess = "2";
    XMPPConnection connection = null;
    Chat chat = null;
    public Login(){
     try{
  connection = new XMPPConnection("192.168.0.208");
  connection.connect();
  connection.login("admin", "admin");
  state = "登入成功";
  System.out.println("登入成功");
  
  }catch(Exception e){
   e.printStackTrace();
  }
    }
  public String loginOn(){
   return state;
  }
  //点击发送消息
  public String sendMeg(){
  System.out.println("发送消息");
  String sendMsg = "";
  try{
  //String messa = request.getParameter("msg");
  chat.sendMessage("hello");
  }catch(Exception e){
   e.printStackTrace();
  }
  return sendMsg;
  }
  //对话框获得消息
  
  public String getMeg(){
  
  chat = connection.getChatManager().createChat("2@of.com", new MessageListener(){
   public void processMessage(Chat chat, Message message) {
    mess = message.getFrom().toString() + " " +  new java.util.Date().toString() + "说:" + message.getBody().toString();
             //这里要把mess显示在textarea中
            System.out.println("来自2的消息:"+mess);
         }
  }
  );
  try{
  }catch(Exception e){
   e.printStackTrace();
  }
  return mess;
  }
 }
%>
<%
Login login = new Login();
%>
<html>
  <head>
    <base href="<%=basePath%>">
        <title>face chat</title>
<script type="text/javascript">
 function login(){
 "<%=login.loginOn()%>"
 }
 function send(){
  var message = document.getElementById("msg").value;
  "<%=login.sendMeg()%>"
 }
 function SubmitKeyClick(obj, evt) { 
         evt = (evt) ? evt : ((window.event) ? window.event : "") 
        keyCode = evt.keyCode ? evt.keyCode : (evt.which ? evt.which : evt.charCode); 
        if (keyCode == 13) { 
           send()
         } 
     } 
 function onbody(){
 window.event.keyCode=13
 }
 
</script>
<script>
function add(){
    document.getElementById("msg").value+="<%=login.mess%>";
    alert("<%=login.mess%>");
   
}
</script>
  </head>
 
  <body onload="onbody()">
    <div>
   <table cellspacing="0" cellpadding="0" border="0" align="center"
    width="100%" height="100%">
    <tbody>
     <tr>
     <%=login.loginOn()%>
     </tr>
     <tr>
     </tr>
     <tr>
      <td></td>
      <td height="100%" align="center" valign="top">
       <div ></div>
       <div >
             <fieldset style="height:15%">
             <table width="100%">
              <tr>
               <td style="width:20%" align="center">
                <button style="height:50;width:50%;" onclick="login();">断开连接</button>
               </td>
               <td style="width:60%">
                <input id="msg" name="msg"  type="text" style="width:100%" style="height:50" onkeydown="if (window.event.keyCode==13)window.event.keyCode=13; return SubmitKeyClick(this,event)" />
               </td>
               <td style="width:20%">
                <button style="height:50;width:50%;" onclick="send();">发送</button>
                <button type="submit" style="height:50;width:50%;" onclick="add();">获取消息</button>
               </td>
              </tr>
             </table>
            </fieldset>
           </div>
            </td>
         </tr>
        </table>
       </div>
      </td>
     </tr>
    </tbody>
   </table>
  </div>
  </body>
</html>

 

当getMsg()监听到消息后...如何把获得的消息体mess实时显示在textarea中???

加载中
0
chuangyu
chuangyu

dwr可以实现你要的push功能

0
Smch_tb
Smch_tb

引用来自#2楼“cyZhu”的帖子

dwr可以实现你要的push功能

对 。。这位朋友说的对。。ajax..里面的框架、、、

0
xf小风
xf小风

需要dwr和java线程来实现,

dwr的反转功能

返回顶部
顶部