温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在java web项目中实现一个聊天功能

发布时间:2020-11-23 15:42:30 来源:亿速云 阅读:415 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关如何在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&#63;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&#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);
 }
 //转发
 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项目中实现一个聊天功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI