本文讲述如何用JAVA语言写一个简易的聊天室

简易聊天室的代码实现,因为笔者还是个正在好好学习努力进步的学生,所以代码可能还有很多可以优化的地方,仅供参考。

开发步骤:

第一部分:UI构建
第一步:编写进入页面的静态页面
第二步:编写聊天主页的静态页面
第三步:将进入页面转成动态页面
第四步:将聊天主页转成动态页面
第二部分:功能实现
第一步:编写用户进入聊天室业务逻辑
第二步:实现将用户列表显示
第三步:实现将聊天内容显示
第四步:实现发送聊天内容

用到三大作用域:
HttpServletRequest,HttpSession,ServletContext

效果图如下:

进入界面:

聊天界面:

需要的代码:

InUI.java(进入页面)
public class InUI extends HttpServlet {private static final long serialVersionUID = 2122558975890099050L;@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//中文编码支持req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");//页面输出对象PrintWriter writer = resp.getWriter();writer.println("<html>");writer.println("<head>");writer.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");writer.println("<title>QG聊天室</title>");writer.println("<style type=\"text/css\">");writer.println("<body background='./ui/g9.jpg' style=\"background-repeat ; background-size:100% 100%   background-attachment:fixed;\">");writer.println("</style>");writer.println("</head>");writer.println("<body>");writer.println("<div style=\"text-align: center; margin: 200px\">");writer.println("<div>");Object msg = req.getAttribute("in_msg");if(msg!=null) {writer.println(msg);}writer.println("<h3>QG聊天室</h3>");writer.println("</div>");writer.println("<div>");writer.println("<form action=\"in.action\" method=\"post\">");writer.println("用户名:<input name=\"username\" size=\"30\"> <input type=\"submit\" value=\"进入\">");writer.println("</form>");writer.println("</div>");writer.println("</div>");writer.println("</body>");writer.println("</html>");}}
MainUI.java
public class MainUI extends HttpServlet {private static final long serialVersionUID = -5468244588883237637L;@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");PrintWriter writer = resp.getWriter();writer.println("<html>");writer.println("<head>");writer.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");writer.println("<title>QG聊天室</title>");writer.println("</head>");writer.println("<frameset cols=\"180,*\">");writer.println("<frame name=\"users\" src=\"users.ui\" noresize=\"noresize\">");writer.println("<frameset rows=\"*,100\">");writer.println("<frame name=\"content\" src=\"content.ui\" noresize=\"noresize\">");writer.println("<frame name=\"message\" src=\"message.ui\">");writer.println("</frameset>");writer.println("</frameset>");writer.println("</html>");}}
ContentUI.java
public class ContentUI extends HttpServlet {private static final long serialVersionUID = 4824128920210671799L;@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");HttpSession session = req.getSession();ServletContext context = req.getServletContext();PrintWriter writer = resp.getWriter();writer.println("<html>");writer.println("<head>");writer.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");writer.println("<meta http-equiv=\"refresh\" content=\"2\">");writer.println("<title>QG聊天室</title>");writer.println("</head>");writer.println("<body>");writer.println("<div>聊天内容</div>");writer.println("<div>");writer.println("<ol>");List<String> contents = (List<String>) context.getAttribute("contents");if(contents!=null) {for(String content:contents) {if(content.contains((String)session.getAttribute("user"))) {writer.println("<li><font color=\"#ff0000\">"+content+"</font></li>");}else {writer.println("<li>"+content+"</li>");}}}writer.println("</ol>");writer.println("</div>");writer.println("</body>");writer.println("</html>");}
MessageUI.java
public class MessageUI extends HttpServlet {private static final long serialVersionUID = 3344260575818484584L;@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");PrintWriter writer = resp.getWriter();writer.println("<html>");writer.println("<head>");writer.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");writer.println("<title>QG聊天室</title>");writer.println("</head>");writer.println("<body>");writer.println("<br />");writer.println("<center>");writer.println("<div>");writer.println("<form action=\"message.action\" method=\"post\">");writer.println("<input name=\"message\" size=\"50\"> <input type=\"submit\" value=\"发送消息\">");writer.println("</form>");writer.println("</div>");writer.println("</center>");writer.println("</body>");writer.println("</html>");}
}
UsersUI.java
public class UsersUI extends HttpServlet {private static final long serialVersionUID = 5378045298264416397L;@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");ServletContext context = req.getServletContext();HttpSession session = req.getSession();PrintWriter writer = resp.getWriter();writer.println("<html>");writer.println("<head>");writer.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");writer.println("<meta http-equiv=\"refresh\" content=\"2\">");writer.println("<title>QG聊天室</title>");writer.println("</head>");writer.println("<body>");writer.println("<div>用户列表</div>");writer.println("<div>");  writer.println("<ul>");List<String> users=(List<String>) context.getAttribute("users");if(users!=null) {for(String username:users) {if(username.equals(session.getAttribute("user"))) {writer.println("<li><font color=\"#ff0000\">"+username+"</font></li>");}else {writer.println("<li>"+username+"</li>");}}}writer.println("</ul>");       writer.println("</div>");writer.println("</body>");writer.println("</html>");}}
InController.java(进入页面的功能实现)
public class InController extends HttpServlet{private static final long serialVersionUID = -7259157146795993735L;@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//中文编码req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");HttpSession session = req.getSession();ServletContext application = req.getServletContext();//获得请求过来的用户名String username = req.getParameter("username");//创建一个集合用于存储用户列表List<String> usernames=(List<String>) application.getAttribute("users");if(usernames==null) {usernames=new ArrayList<>();}boolean flag=false;for(String u:usernames) {if(u.equals(username)) {flag=true;break;}}if(flag==false) {//将用户名放在集合里面usernames.add(username);//将集合放回ServletContext容器里面application.setAttribute("users", usernames);session.setAttribute("user", username);resp.sendRedirect(req.getContextPath()+"/main.ui");}else {req.setAttribute("in_msg", "用户已经存在");req.getRequestDispatcher("/in.ui").forward(req, resp);        }}}
MessageController.java
public class MessageController extends HttpServlet{private static final long serialVersionUID = -8163731608077492143L;@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");HttpSession session = req.getSession();ServletContext application = req.getServletContext();String message = req.getParameter("message");List<String> contents=(List<String>) application.getAttribute("contents");if(contents==null) {contents=new ArrayList<>();}String msg="["+session.getAttribute("user")+"]:"+message;contents.add(msg);application.setAttribute("contents", contents);resp.sendRedirect(req.getContextPath()+"/message.ui");}}

END

用eclipse和Tomcat写一个简易聊天室相关推荐

  1. 撸一个简易聊天室,不信你学不会实时消息推送(附源码)

    点击上方 好好学java ,选择 星标 公众号重磅资讯,干货,第一时间送达 今日推荐:推荐 19 个 github 超牛逼项目!个人原创100W +访问量博客:点击前往,查看更多 分不清轮询.长轮询? ...

  2. 连夜撸了一个简易聊天室

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 分不清轮询.长轮询?不知道什么时候该用websocket还 ...

  3. WebSocket+Tomcat实现网页简易聊天室

    简单的说一下什么是websocket,它是基于TCP的服务器与客户端之间全双工通信的一种协议,允许服务端主动推送消息给客户端,只需要一次握手,就可以在服务端和客户端之间建立持久相连接,在这里我们只用到 ...

  4. Netty+Android搭建一个简易聊天室(实现群聊和私聊)

    零,前言 JRBM项目中无论是好友私聊,公开聊天室,还是比赛平台都需要用到长连接,之前没有接触过网络通信等知识,更别说框架了,因此直接上手netty确实有些困难,在前期主要是在b站上看(https:/ ...

  5. Socket编程实现简易聊天室

    1.Socket基础知识 Socket(套接字)用于描述IP地址和端口,是通信链的句柄,应用程序可以通过Socket向网络发出请求或者应答网络请求. Socket是支持TCP/IP协议的网络通信的基本 ...

  6. 简单的一个在线聊天室

    闲着无聊写一个基于数据库写一个在线聊天室 前几天闲着没事突然想起写个小程序玩玩,就想到了写个在闲聊天室,话不多说直接开始. 1.第一当然是先写一个聊天界面啦,我写的界面是这样: 聊天页面 2.写这个程 ...

  7. java实现简易聊天窗口先运行服务器还是客户端_一个简易聊天功能的服务器端和客户端源码...

    学习完J2SE可以写一个简易的聊天软件来让刚学的知识融会贯通,代码注释的很详细哦! 开发版本历程: V0.1:客户端实现一个界面 V0.2:客户端界面有输入框和显示框的界面 V0.3:客户端关闭按钮可 ...

  8. linux shell 计算器 除0,用shell写一个简易计算器,可以实现加、减、乘、除运算,假如脚本名字为1.sh,执行示例:./1....

    用shell写一个简易计算器,可以实现加.减.乘.除运算,假如脚本名字为1.sh,执行示例:./1.sh 1 + 2#!/bin/bash if [ $# -ne 3 ] then echo &quo ...

  9. 肝一波 ~ 手写一个简易版的Mybatis,带你深入领略它的魅力!

    零.准备工作 <dependencies><dependency><groupId>mysql</groupId><artifactId>m ...

最新文章

  1. java非阻塞 串口读数据_串口阻塞与非阻塞
  2. 赠票 | 重磅揭晓Flink Forward Asia 2019完整议程!
  3. linux显示进程的h开头的,Linux上进程的开始时间
  4. 2018-2019-1 20165237 《信息安全系统设计基础》第四周学习总结
  5. Vue绑定数据v-bind缩写:字段名 双向绑定v-model缩写:model 监听动作v-on缩写@ 记住:与数据相关用冒号 与动作相关用@
  6. 使用Oracle的审计功能监控数据库中的可疑操作
  7. 笔记本电脑有蓝牙连接功能吗_百元蓝牙无线键盘推荐——罗技K380
  8. 个人认为不错的句子(part2)--计算机是一种工具,但是如果没有绝对精确的指令,计算机也将一无是处
  9. 《长调》 :寻找我们共同失去的天堂
  10. 各种平台的表达芯片跟mRNA-seq数据比较
  11. ios沙盒查找图片展示
  12. Range的学习笔记
  13. 辽宁移动客服呼叫中心两级质检管理效果佳
  14. ensp(华为VRRP配置)
  15. 2019年CVTE凉
  16. 【金融财经】金融市场一周简报(2017-08-25)
  17. 计算机主板北桥芯片的主要作用,介绍一下南北桥芯片的位置及作用
  18. UE5 Metahuman使用Live Link Face动画不匹配的问题修复
  19. 小米手机 加载桌面 失败 黑屏
  20. Python网络爬虫:利用正则表达式方法爬取‘’豆瓣读书‘’中‘’新书速递‘’条目

热门文章

  1. 安卓变身linux,能让 Android 设备变身Linux启动盘DriveDroid
  2. 符合自然的饮食睡眠是最好的养生
  3. php 支付宝wap接口,呕心之作:支付宝的手机网站支付接口的应用,呕心之作_PHP教程...
  4. 2021 年百度之星·程序设计大赛 - 初赛一(1006/毒瘤数据结构题)
  5. 确定CORONA影像获取时间的记录
  6. 网络攻击-arp攻击
  7. 电脑的桌面计算机在哪里,笔记本电脑上面的计算机在哪里?—详细步骤
  8. 如何提高电脑运行速度
  9. vsftp日志查看_vsftp日志内容详解
  10. Unity --- 虚拟轴的使用