好學驛站

          JSP、JAVA、struts、hibernate、spring、eclipse、工作流、電子政務、協同辦公、J2ME

          頁面嵌入在線聊天 (prototype)

          問題 : 提供給B/S 結構一個實時的運行展現
          解決:?? prototype 的?Ajax.PeriodicalUpdater 更新
          ??????????? prototype 的??? Ajax.Request 語句提交
          ????????????action 語句的組織展現

          http://www.aygfsteel.com/Files/Good-Game/take.rar

          ????沒有lib 大家可以就加 struct 相關 lib 就可以了

          說明:?
          ??????

          <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
          <html>
          ??
          <head>
          ??????????
          <SCRIPT?language="Javascript"?src="prototype1.5.0.js"></SCRIPT>
          ??
          </head>
          ??
          ??
          <body?onload="Said()">?
          ??????
          <INPUT?id='name1'?type="text"?size="10"?value="請輸入名字"?onfocus="if($('name1').value=='請輸入名字')?$('name1').innerText=''?"?/>
          ??????
          <INPUT?id='text1'?type="text"?onkeydown="if(event.keyCode==13){event.keyCode=0;?onb();}"/>
          ??????
          <INPUT?id='button1'?type="button"?value="test"?onclick='onb()'?/>
          ??
          </body>
          ??
          <div?id='div1'/>
          </html>
          <script?type="text/javascript"??>
          ??????? //語句異步提交給服務器 Ajax.Request
          ????
          function?onb(){?????????????????????????????????????????
          ??????
          if($('name1').value=='請輸入名字'?||?$('name1').value==''){
          ??????????window.alert('請輸入名字');
          ??????????
          return?
          ??????}

          ??????
          ??????
          var?url?=?'http://192.168.1.130:7000/xxs/mySaid.do?name='+$F('name1')+'&take='+$F('text1');????????????????
          ??????new?Ajax.Request(
          ????????url,
          ????????
          {
          ????????????method:?'get',
          ????????????onComplete:?action??????????????
          ????????}
          );
          ????????
          ????????$('name1').readOnly?
          =?true
          ????????$('name1').style.background?
          =?'lightblue'
          ????????$('text1').innerText
          =''?
          ????}

          ?????? //局部刷新 并提取 語句
          ????
          function?Said(){
          ????????
          var?url?=?'http://192.168.1.130:7000/xxs/mySaid.do';??
          ????????new?Ajax.PeriodicalUpdater(
          ????????????'div1',
          ????????????url,
          ????????????
          {
          ????????????????method:?'get',
          ????????????????onComplete:?SumSaid,
          ????????????????evalScripts:?
          true,
          ????????????????frequency:?
          1,
          ????????????????decay:?
          1
          ????????????}

          ????????);
          ????}

          ?????? //展現
          ????
          function?SumSaid(req){
          ????????$('div1').innerHTML?
          =?req.responseText;
          ????}


          ????
          function?action(req){
          ????}

          ????
          </script>
          ????

          Action
          ?? private static int SIZE = 30 ;
          ???? private List list = new ArrayList();

          ????
          public?ActionForward?execute(
          ????????ActionMapping?mapping,
          ????????ActionForm?form,
          ????????HttpServletRequest?request,
          ????????HttpServletResponse?response)?
          {
          ????????String?take?
          =?request.getParameter("take")?;
          ????????String?name?
          =?request.getParameter("name")?;
          ????????response.addHeader(
          "Cache-Control","no-cache");?
          ????????
          ????????request.setAttribute(
          "said",DueiLie(name,take));

          ????????
          try?{
          ????????????request.setCharacterEncoding(
          "gb2312");
          ????????????response.setCharacterEncoding(
          "gb2312");
          ????????}
          ?catch?(Exception?e)?{
          ????????}


          ????????
          return?mapping.findForward("list");
          ????}

          ?????
          ??????? //語句的組織
          ????
          public?String?DueiLie(String?name,String?take){
          ????????
          ????????StringBuffer?strb??
          =?new?StringBuffer();
          ????????
          if(name==null||take==null);
          ????????
          else{
          ????????????strb.append(?name?).append(
          "?:?").append(take).append("</br>")?;
          ????????????
          if(list.size()==SIZE){
          ????????????????list.remove(list.get(
          0));
          ????????????????list.add(strb.toString()?);
          ????????????}
          else{
          ????????????????list.add(?strb.toString()?);
          ????????????}

          ????????}

          ????????StringBuffer?takes?
          =?new?StringBuffer();
          ????????Iterator?it?
          =?list.iterator();
          ????????
          while(it.hasNext()){
          ????????????takes.insert(?
          0,(String)it.next()?);
          ????????}

          ????????
          return?takes.toString();
          ????}

          jsp ( list ) 中文處理 jsp 就一句話
          <%= new String(((String)request.getAttribute("said")).getBytes("iso8859-1"),"GBK") %>

          posted on 2007-06-13 11:07 好學蜘蛛 閱讀(336) 評論(0)  編輯  收藏 所屬分類: JAVAstruts 、AJAX WEB2.0

          主站蜘蛛池模板: 定州市| 全南县| 呈贡县| 定边县| 札达县| 台北市| 连山| 北流市| 涞水县| 渑池县| 兴海县| 报价| 宜宾县| 环江| 抚顺县| 瑞安市| 南雄市| 邵阳县| 汕头市| 桂阳县| 通山县| 靖江市| 岳西县| 宁河县| 加查县| 安福县| 益阳市| 固安县| 大竹县| 文登市| 通渭县| 通城县| 富川| 桑植县| 建德市| 汝城县| 安远县| 遂昌县| 华宁县| 新乐市| 永济市|