本人最近在學習一個javaweb聊天項目,在這個項目中所要實現的功能比較簡單:實現登陸之后可以在里面進行選擇對象進行聊天,在線人數和在線人員名稱都可以即時顯示,信息message可以時時更新在每一個登錄頁面中,管理員admin具有把普通用戶user踢下線的功能,而自己剛剛整合了SSM(struts+spring+mybatis)框架,並且通過maven進行jar包管理,在這里雖說不能很好體現框架的優勢,但也是麻雀雖小五臟俱全。下圖是項目后台的文件列表:
在這里,通過定義的監聽器Mylistener使其在項目初始化的時候便聲明一個Map<user,httpsession>,放在application中,用於在用戶登錄時保存用戶相關信息,在管理員將用戶踢下線的時候將Map中相對應的session的 invalidate()銷毀並通過map.remove()清除即可,以下放在webchartAction中登錄功能login()代碼:
public String login() throws IOException{ List<user> list=new ArrayList<user>(); String username=request.getParameter("username"); String password=request.getParameter("password"); user user=new user(); user.setPassword(password); user.setUsername(username); session.setAttribute("username", username); list=userService.login(user); String message=""; if (null!=application.getAttribute("message")) { message=application.getAttribute("message") .toString(); } System.out.println(username+"進來了"); String i=username ; Map<user,HttpSession> userMap=(Map<user, HttpSession>) application.getAttribute("userMap"); if(list.size()>0){ String kk=list.get(0).getType(); session.setAttribute("type", kk); //判斷list.get(0),即當前登錄用戶是否已經登錄,若已經登錄則不增加在線人數,並且不再重新保存session if (userMap.containsKey(list.get(0))!=true) { userMap.put(list.get(0), session); application.setAttribute("userMap", userMap); //記錄在線人數 int j=userMap.entrySet().size(); application.setAttribute("mapcount",j); } message+= "系統公告:<font color='gray'>" + user.getUsername() + "走進了聊天室!</font><br>"; application.setAttribute("message", message); return "login_success"; } else return "login_fail"; }
管理員踢下線功能代碼:
public String kick() throws IOException{ // 1.接收參數 System.out.println("管理員要踢人啦"); String username = request.getParameter("username"); List<user> list=userService.queryUserList(username); user user1=list.get(0); System.out.println(user1); // 2.踢人:從userMap中將用戶對應的session銷毀. // 獲得userMap集合(在線列表) Map<user, HttpSession> userMap = (Map<user, HttpSession>)application.getAttribute("userMap"); // 獲得這個用戶對應的session.如何知道是哪個用戶呢? id已經傳遞過來.去數據庫中查詢. // 重寫user的equals 和 hashCode 方法 那么只要用戶的id相同就認為是同一個用戶 // 從map集合中獲得用戶的對應的session HttpSession session = userMap.get(user1); // 銷毀session session.invalidate(); //從usermap中將此用戶刪除 userMap.remove(user1); //刷新在線人數 int j=userMap.entrySet().size(); application.setAttribute("mapcount",j); // 通過跳轉到登錄界面 return "login_success"; }
下圖是前端的文件列表
以下是前端聊天界面的javascript中的代碼
<script type="text/javascript"> var sysBBS = "<span style='font-size:14px; line-height:30px;'>歡迎光臨心之語聊天室,請遵守聊天室規則,不要使用不文明用語。</span><br><span style='line-height:22px;'>"; window.setInterval("showContent();",1000); window.setInterval("showOnLine();",1000); window.setInterval("check();",1000); // 相當於window.onload $(function(){ showOnLine(); showContent(); check(); }); function check(){ $.post("<%=path%>/webchart/webchart_check.action",function(data){ if(data == 1){ // 提示用戶下線了 alert("用戶已經被踢下線了!"); // 回到登錄頁面!通過action跳轉 window.location="webchart_loginfails.action"; } }); } // 顯示在線人員列表 function showOnLine(){ // 異步發送請求 獲取在線人員列表 // Jquery發送異步請求 $.post("<%=path%>/online.jsp?"+new Date().getTime(),function(data){ // $("#online") == document.getElementById("online"); $("#online").html(data); }); } // 顯示聊天的內容 function showContent(){ $.post("<%=path%>/webchart/webchart_getMessage.action",function(data){ $("#content").html(sysBBS+data); }); } function set(selectPerson){ //自動添加聊天對象 if(selectPerson != "<%=session.getAttribute("username")%>"){ form1.to.value=selectPerson; }else{ alert("請重新選擇聊天對象!"); } } function send(){ if(form1.to.value==""){ alert("請選擇聊天對象!"); return false; } if(form1.content.value==""){ alert("發送信息不可以為空!"); form1.content.focus(); return false; } // $("#form1").serialize():讓表單中所有的元素都提交. // jquery提交數據.{id:1,name:aa,age:25} $.post("<%=path%>/webchart/webchart_sendMessage.action",$("#form1").serialize(),function(data){ $("#content").html(sysBBS+data+"哈哈"+"</span>"); }); } function exit(){ alert("歡迎您下次光臨!"); window.location.href="<%=path%>/index.jsp"; } function checkScrollScreen(){ if(!$("#scrollScreen").attr("checked")){ $("#content").css("overflow","scroll"); }else{ $("#content").css("overflow","hidden"); //當聊天信息超過一屏時,設置最先發送的聊天信息不顯示 //alert($("#content").height()); $("#content").scrollTop($("#content").height()*2); } setTimeout('checkScrollScreen()',500); } </script>
在上面的代碼中,window.setInterval()是保證頁面在一定時間間隔內自動刷新,這樣保證信息的及時顯現,id為“content”的便是信息sysBBS+message展示頁面;
以下代碼是online.jsp中的table代碼,用於展現現在人員的信息,並且通過判斷用戶的type是否為admin來決定是否可以出現踢下線的功能;
在這個項目中,因為剛開始做這些小項目,我遇到的問題有很多,列出幾條:
- 信息及時刷新問題,雖說通過window.setInterval()解決了,但是總感覺這樣比較浪費資源,不知道是否可以有更好的辦法去解決這個問題;
- Map中的鍵值對<user,httpsession>獲取問題,通過這個可以保存登陸者的很多信息,包括session,但是在編碼的時候發現獲取了user,也就是sessionid,但是卻不能獲取相對應的session,后來通過百度查資料才發現需要重寫實體類中的tostring()和Hascode()方法,不然讀取的session一直為空,這樣在銷毀的時候就會報錯。
- 在這個項目中,前期一直用火狐瀏覽器進行測試,后來在判斷用戶類型是否為admin而可以有踢下線的功能的時候,在同一個瀏覽器中的session一直被最近一個登錄者的session刷新,導致判斷的用戶type一直是顯示最近登陸的用戶type,當用兩個不同瀏覽器登陸的時候,這個問題就得到了解決,管理員可以刪除普通用戶,而普通用戶不可以,我猜測這應該是session共享問題,在同一台機器的同一個瀏覽器測試不同登錄人員登錄同一個項目,之前的session會被后者登錄所用的session給沖掉,這個問題需要注意;
首次發博客,內容表述不夠嚴謹,如果所講述內容有二解,望能共同探討;