#1.頁面組成css
//manage.jsp <table> <tr style="height: 10%"> <th colspan="2">NBA直播室後臺</th> </tr> <tr style="height: 83%"> <td colspan="2"><jsp:include page="content.jsp" flush="true"></jsp:include></td> </tr> <tr style="height: 7%"> <td style="85%"><input type="text" name="content" class="content" id="content"></td> <td><input type="button" value="發表" class="btn"></td> </tr> </table>
//content.jsp <body> <textarea id="showMsg" style="width:100%;height:100%;resize:none;font-size:16px;readonly:true" ></textarea> </body>
//index.jsp <table> <tr style="height: 10%"> <th colspan="2">NBA直播室</th> </tr> <tr style="height: 90%"> <td colspan="2"><jsp:include page="manage/content.jsp" flush="true"></jsp:include></td> </tr> </table>
$(function(){ $(".btn").click(function(){ var content = $(".content").val(); if(validate()){ $.ajax({ method:"post", url:"LivingRoom", //data包括內容,以及一個標記,由於咱們還有一個是要展現,一樣請求同一個url,加入r是爲了不網頁緩存 data:"content="+content+"&flag=send"+"&r="+new Date(), dataType:"json", success:function(data){ //接收後臺返回的值 if(data == 1){ //清除輸入框中的內容並得到焦點 $(".content").val("").focus(); } }, error:function(){ alert("數據提交錯誤,請稍後再試!"); } }); } }); }); //驗證函數 function validate(){ var reg = "/^\s+|\s+$/g"; var content = document.getElementById("content").value; if(content.replace("reg","").length<1){ alert("須要輸入內容後,才能發表!"); return false; } return true; }
String flag = request.getParameter("flag"); //獲取存儲文件的根路徑 String path = request.getSession().getServletContext().getRealPath("record"); SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd"); String fileName = sdf.format(new Date())+".dat"; path = path + "/" + fileName; if("send".equals(flag)){ send(request,response,path); }else if("show".equals(flag)){ show(request,response,path); }
//得到傳送過來的內容 String content = request.getParameter("content"); //輸出 PrintWriter out = response.getWriter(); //拼接內容 SimpleDateFormat sdf = new SimpleDateFormat("[HH:mm:ss]"); content = sdf.format(new Date()) + ":" + content; //定義文件輸出流 BufferedWriter bw = null; //向record目錄中寫入 try { bw = new BufferedWriter(new FileWriter(path,true)); bw.write(content); bw.newLine(); bw.flush(); //若是成功 out.println(1); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); }finally{ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
//得到展現後臺輸入內容效果 $(function(){ function show(){ $.ajax({ method:"get", url:"LivingRoom?flag=show", data:null, dataType:"text", success:function(data){ $("#showMsg").val(data); }, error:function(){ alert("數據請求錯誤,請稍後再試!"); } }); } //設置每隔500ms函數執行一次,達到數據快速更新 setInterval(show,500); });
//讀取文件中的內容,返回到頁面 //設置咱們返回數據字符類型,不這樣,網頁會出現亂碼 response.setCharacterEncoding("utf-8"); StringBuilder builder = new StringBuilder(); BufferedReader br = null; PrintWriter pw = response.getWriter(); try { br = new BufferedReader(new FileReader(path)); String msg = ""; while((msg = br.readLine()) != null){ builder.append(msg+"\r\n"); } pw.println(builder.toString()); pw.flush(); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }