您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何在java web項目中實現一個聊天功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
實現思路
1.登錄頁面 login.jsp 輸入昵稱
2.編寫一個LoginSevlet,處理登錄提交的昵稱,保存登錄昵稱到session(會話變量)
若昵稱合法 if(nickname.length()!=0),跳轉到聊天室,不合法回到登錄頁面
3.編寫聊天室頁面chatroom.jsp ,是一個框架,把多個頁面集成到一個頁面中
<iframe src="message.jsp" width="100%" height="70%"> </iframe> <iframe src="input.jsp" width="100%" height="25%"> </iframe>
4.聊天內容顯示message.jsp,不斷自動刷新標簽內設置響應頭,用${上下文變量}顯示聊天內容
<meta http-equiv="refresh" content="1">
5.input.jsp(不刷新)輸入聊天內容文本框text,點擊發送聊天內容按鈕時,將把文本框的聊天信息提交給ChatServlet處理。下方有超鏈接可退出聊天室。
<a href="login.jsp" target="_top">退出聊天室</a>
6.ChatServlet:檢測聊天信息合法性,把聊天信息加入到ServletContext變量(字符串)中,(每次都將xx:xxxx加入上下文字符串)
判斷是否存在該變量,存在則直接讀取現有聊天內容,把數據加入進去后,重新寫回上下文變量
7.實現拍一拍功能(目的:學習如何進行鏈接操作)
每個人名做成一個鏈接,訪問某一個servlet,該servlet可以在聊天信息(上下文變量)里添加一行,xx拍了yyy一下,跳轉回message.jsp
問題:鏈接里應該包含yyy參數,servlet?nick=yyy
say = "<a href='NickServlet?nickname="+nickname+"'target='_parent'>"+nickname+"</a>"+":"+text;
8.多人聊天實現:多種類型瀏覽器
關鍵代碼
login.jsp
昵稱:<input type="text" name="nickname"/><br> <br><br> <input type="submit" value="登錄"/> <input type="reset" value="取消"/>
input.jsp
<form action="input.do" method="post"> <input type="text" size="50" name="text"/> <input type="submit" value="發送"><br><br><br><br> <a href="login.jsp" rel="external nofollow" rel="external nofollow" target="_top">退出聊天室</a> </form>
message.jsp
<h4>一起來聊天吧</h4> ${says}
LoginServlet.java
package chatting; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; @WebServlet(name = "LoginServlet2",urlPatterns = {"/login.do"}) public class LoginServlet2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //避免亂碼 response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); //nickname保存用戶昵稱 String nickname = request.getParameter("nickname"); //nickname存儲到session變量中 HttpSession session = request.getSession(); session.setAttribute("nickname",nickname); //對nickname進行合法性判斷 if(nickname.length()!=0){ //昵稱合法,則進入聊天室頁面 request.getRequestDispatcher("/chatroom.jsp").forward(request,response); } else{ //昵稱不合法,返回登陸頁面 request.getRequestDispatcher("/login.jsp").forward(request,response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
ChatServlet.java
package chatting; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.util.Vector; @WebServlet(name = "ChatServlet",urlPatterns = "/input.do") public class ChatServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //防止亂碼 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); HttpSession session = request.getSession(); String text = request.getParameter("text"); //text存儲聊天信息,并進行合法性判斷 if (text.length()!=0) { String say =new String(); //獲取session中的昵稱 String nickname = (String)session.getAttribute("nickname"); //將昵稱作為鏈接,并傳參 say = "<a href='NickServlet?nickname="+nickname+"'target='_parent'>"+nickname+"</a>"+":"+text; String says = new String(); //將昵稱和聊天信息一起放在上下文變量 ServletContext context = getServletContext(); if(context.getAttribute("says")!=null){ says = context.getAttribute("says") + say + "<br>"; } else{ says = say + "<br>"; } context.setAttribute("says", says); request.setAttribute("says", says); } //轉發 RequestDispatcher dispatcher = request.getRequestDispatcher("/input.jsp"); dispatcher.forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
NickServlet.java
package chatting; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; @WebServlet(name = "NickServlet",urlPatterns = "/NickServlet") public class NickServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("utf-8"); //將拍一拍放到上下文變量says中 一起輸出 HttpSession session = request.getSession(); //nickname存儲當前用戶昵稱 String nickname = (String)session.getAttribute("nickname"); //name存儲連接中傳遞的參數,即被拍的用戶昵稱 String name = java.net.URLDecoder.decode(request.getParameter("nickname"),"UTF-8"); String pick = nickname+"拍了拍"+name+"<br>"; ServletContext context = request.getServletContext(); String says = context.getAttribute("says")+pick; context.setAttribute("says",says); RequestDispatcher dispatcher = request.getRequestDispatcher("/chatroom.jsp"); dispatcher.forward(request, response); } }
關于如何在java web項目中實現一個聊天功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。