溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在java web項(xiàng)目中實(shí)現(xiàn)一個(gè)聊天功能

發(fā)布時(shí)間:2020-11-23 15:42:30 來源:億速云 閱讀:409 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何在java web項(xiàng)目中實(shí)現(xiàn)一個(gè)聊天功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

實(shí)現(xiàn)思路

1.登錄頁面 login.jsp 輸入昵稱

2.編寫一個(gè)LoginSevlet,處理登錄提交的昵稱,保存登錄昵稱到session(會(huì)話變量)
若昵稱合法 if(nickname.length()!=0),跳轉(zhuǎn)到聊天室,不合法回到登錄頁面

3.編寫聊天室頁面chatroom.jsp ,是一個(gè)框架,把多個(gè)頁面集成到一個(gè)頁面中

<iframe src="message.jsp" width="100%" height="70%"> </iframe>
<iframe src="input.jsp" width="100%" height="25%"> </iframe>

4.聊天內(nèi)容顯示message.jsp,不斷自動(dòng)刷新標(biāo)簽內(nèi)設(shè)置響應(yīng)頭,用${上下文變量}顯示聊天內(nèi)容

<meta http-equiv="refresh" content="1">

5.input.jsp(不刷新)輸入聊天內(nèi)容文本框text,點(diǎn)擊發(fā)送聊天內(nèi)容按鈕時(shí),將把文本框的聊天信息提交給ChatServlet處理。下方有超鏈接可退出聊天室。

<a href="login.jsp" target="_top">退出聊天室</a>

6.ChatServlet:檢測(cè)聊天信息合法性,把聊天信息加入到ServletContext變量(字符串)中,(每次都將xx:xxxx加入上下文字符串)
判斷是否存在該變量,存在則直接讀取現(xiàn)有聊天內(nèi)容,把數(shù)據(jù)加入進(jìn)去后,重新寫回上下文變量

7.實(shí)現(xiàn)拍一拍功能(目的:學(xué)習(xí)如何進(jìn)行鏈接操作)
每個(gè)人名做成一個(gè)鏈接,訪問某一個(gè)servlet,該servlet可以在聊天信息(上下文變量)里添加一行,xx拍了yyy一下,跳轉(zhuǎn)回message.jsp
問題:鏈接里應(yīng)該包含yyy參數(shù),servlet?nick=yyy

say = "<a href='NickServlet&#63;nickname="+nickname+"'target='_parent'>"+nickname+"</a>"+":"+text;

8.多人聊天實(shí)現(xiàn):多種類型瀏覽器

關(guān)鍵代碼

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="發(fā)送"><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存儲(chǔ)到session變量中
 HttpSession session = request.getSession();
 session.setAttribute("nickname",nickname);
 //對(duì)nickname進(jìn)行合法性判斷
 if(nickname.length()!=0){
 //昵稱合法,則進(jìn)入聊天室頁面
  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存儲(chǔ)聊天信息,并進(jìn)行合法性判斷
 if (text.length()!=0) {
  String say =new String();
  //獲取session中的昵稱
  String nickname = (String)session.getAttribute("nickname");
  //將昵稱作為鏈接,并傳參
  say = "<a href='NickServlet&#63;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);
 }
 //轉(zhuǎn)發(fā)
 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存儲(chǔ)當(dāng)前用戶昵稱
 String nickname = (String)session.getAttribute("nickname");
 //name存儲(chǔ)連接中傳遞的參數(shù),即被拍的用戶昵稱
 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);
 }
}

關(guān)于如何在java web項(xiàng)目中實(shí)現(xiàn)一個(gè)聊天功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI