实现简单聊天室。要求以B/S方式实现多人聊天,可以保留聊天者的昵称和聊天内容。
主要由7个jsp页面组成

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
  </head>
  
  
      <frameset rows="80%,*">
          <frameset cols="20%,*">
              <frame src="list.jsp"/>
              <frame src="msg.jsp"/>             
          </frameset>
          <% response.setHeader("refresh", "10"); %>
          <frame src="send.jsp"/>
      </frameset>
 
</html>

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'login.jsp' starting page</title>
  </head>
  <body>
      <%
          String msg = (String) request.getAttribute("msg");
          if(msg == null){
              msg = "";
          }
       %>
       <p><%=msg %></p>
       <form action="deal_login.jsp" method="post" name="login_form">
           <p align="center" >用户名<input type="text" name="username"/></p>
           <p align="center"><input type="submit" value="登入"/></p>
       </form>
  </body>
</html>

deal_login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>My JSP 'deal_login.jsp' starting page</title>
  </head>
  <body>
      <%
          request.setCharacterEncoding("utf-8");
          
          //获取login.jsp表单中提交过来得值
          String username = request.getParameter("username");
          //判断用户名是否为空
          if(username.trim().length() == 0){
              request.setAttribute("msg", "用户名不得为空");
              request.getRequestDispatcher("login.jsp").forward(request,response);
              return ;
              
          }
          
          //从application作用域中取出用户列表
          List<Object> users = (List<Object>) application.getAttribute("users");
          
          //如果没有用户列表,进行实例化
          if(users == null){
              users = new ArrayList<Object>();
              
          }
          //查看当前列表中是否包含当前得登录用户
          if(users.contains(username)){
              //包含登入用户,设置提醒信息
              request.setAttribute("msg", "该用户已登入,请重新登入");
              request.getRequestDispatcher("login.jsp").forward(request,response);
              return;
          }
          
          //将登入用户的用户名加入该用户列表
          users.add(username);
          application.setAttribute("users", users);
          session.setAttribute("user", username);
          response.sendRedirect("main.jsp");
       %>
  </body>
</html>

loginOff.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>My JSP 'loginOff.jsp' starting page</title>

  </head>
  
  <body>
      <%
          //从application作用域中读取用户列表
          List<Object> users = (List<Object>)application.getAttribute("users");
          String username = (String) session.getAttribute("user");
          //从该用户列表中移除该用户
          users.remove(username);
          application.setAttribute("users",users);
          //注销该用户的会话
          session.invalidate();
       %>
       <jsp:forward page="login.jsp"/>
  </body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>My JSP 'list.jsp' starting page</title>
  </head>
  <body>
  <%
      //设置刷新间隔
      response.setHeader("refresh", "1");
      //从application作用域中读取用户列表
      List<Object> users = (List<Object>) application.getAttribute("users");
      out.print("目前在线的人有:"+ users.size()+"人");
      for(int i=0;i<users.size();i++){
          String username = (String) users.get(i);
          out.print("<li>"+username);
      }
   %>
  
  </body>
  
</html>

msg.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
      <%
          response.setHeader("refresh", "1");
          
          List<Object> msgs = (List<Object>) application.getAttribute("msgs");
          List<Object> to_sb = (List<Object>) application.getAttribute("private");
          List<Object> from_sb = (List<Object>) application.getAttribute("S_private");
          
          String user = (String)session.getAttribute("user");
          
          try{
              for(int i=0;i< msgs.size();i++){
                  String t1 = (String) msgs.get(i);
                  String t2 = (String) to_sb.get(i);
                  String t3 = (String) from_sb.get(i);
                  
                  if(t2.equals(user) || t2.equals("所有人") || t3.equals(user)){
                      out.print(t1);
                  }
              }
          }catch(Exception e){}
       %>
  </body>
</html>

send.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
      <form action="" method="post">
          <p align="right">
          <%=session.getAttribute("user") %>:
                  发送消息给:
          <select name="select">
              <option value="0">所有人</option>
              <%
                  List<Object> users = (List<Object>) application.getAttribute("users");
                  for(int i=0;i<users.size();i++){
                      String username = (String) users.get(i);%>
                      <option value="<%=i+1 %>"><%=username %></option>
                  <%}
               %>
          </select>
          <input type="text" name="message" size="50" /><br>
          <input type="submit" value="发送"/>
          <input type="button" value="退出登录" onClick="parent.location.href='loginOff.jsp'">
      </form>
      
      <%
          String user = (String) session.getAttribute("user");
          request.setCharacterEncoding("utf-8");
          String message = request.getParameter("message");
          
          String to="";
          try{
              //被发送信息方
              List<Object> tmp = (List<Object>) application.getAttribute("users");
              String SS = request.getParameter("select");
              int a = Integer.parseInt(SS);
              if(a==0){
                  to="所有人";
                  
              }
              else{
                  to=(String)tmp.get(a-1);
                  
              }
              
          }catch(Exception e){}
          
          List<Object> msgs = (List<Object>) application.getAttribute("msgs");
          if(msgs == null){
              msgs = new ArrayList<Object>();
          }
          List<Object> to_sb = (List<Object>) application.getAttribute("private");
          if(to_sb == null){
              to_sb = new ArrayList<Object>();
          }
          List<Object> from_sb = (List<Object>) application.getAttribute("S_private");
          if(from_sb == null){
              from_sb = new ArrayList<Object>();
          }
          
          if(message != null && !message.equals("")){
              msgs.add(user+"发送信息给"+to+":" +message +"<br/>");
              to_sb.add(to);
              from_sb.add(user);
              
              application.setAttribute("msgs", msgs);
              application.setAttribute("private", to_sb);
              application.setAttribute("S_private", from_sb);
          }
       %>
  </body>
</html>

实验效果:
在这里插入图片描述

Logo

致力于链接即构和开发者,提供实时互动和元宇宙领域的前沿洞察、技术分享和丰富的开发者活动,共建实时互动世界。

更多推荐