/CHATROOM/WebContent/doLogin.jsp:
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // 设置编码格式 request.setCharacterEncoding("UTF-8"); // 获取登录的用户名 String userName = request.getParameter("account"); // 从application对象中把存储的用户列表获取到 List<String> userList = (List<String>) application.getAttribute("userList"); // 判断你否为第一个登录该系统 if(userList==null) { // 创建存储在线用户列表对象集合 userList = new ArrayList<String>(); } // 判断当前登录用户名是否已存在 if(userList.contains(userName)) { out.print("<script>alert('该用户已经登录,使用其他用户名登录');location.href='login.jsp';</script>"); return; } // 把当前登录的用户存储在在线用户列表集合中 userList.add(userName); // 把在线用户列表存储在application对象中 application.setAttribute("userList", userList); // 把当前登录用户存储到session对象中 session.setAttribute("currentUser", userName); // 跳转到聊天主页 response.sendRedirect("index.jsp"); %>
/CHATROOM/WebContent/doSend.jsp:
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% // 设置编码 request.setCharacterEncoding("UTF-8"); // 获取用户发送消息 String message = request.getParameter("message"); // 获取发送用户名称 String userName = (String)session.getAttribute("currentUser"); // 组合消息文本 String content = "<font color='blue'>"+userName+"说:</font><br/> "+message+"<br/><br/>"; // 获取存储在application当中已经发送过消息集合 List<String> messages = (List<String>)application.getAttribute("msglist"); // 判断是否为第一发送信息的用户 if(messages == null) { messages = new ArrayList<String>(); } // 在把当前用户发送的信息存储到消息列表集合中 messages.add(content); // 在把新的信息列表存储到application中 application.setAttribute("msglist", messages); // 跳转到index.jsp页面 response.sendRedirect("sendMessage.jsp"); %>
/CHATROOM/WebContent/index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <link href="css/main.css" type="text/css" rel="stylesheet" /> </head> <body> <center> <table width="50%" border="1px" cellpadding="0" cellspacing="0"> <caption>YC59公共聊天室</caption> <tr> <td rowspan="2" height="500px" width="26%"> <iframe src="userlist.jsp" align="top" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe> </td> <td height="360px"> <iframe align="top" src="showMessageList.jsp" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe> </td> </tr> <tr> <td> <iframe src="sendMessage.jsp" align="top" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe> </td> </tr> </table> </center> </body> </html>
/CHATROOM/WebContent/login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <link href="css/main.css" type="text/css" rel="stylesheet" /> </head> <body> <fieldset> <legend>英才59班聊天室登录</legend> <form action="doLogin.jsp" method="post"> <table> <tr> <th>用户名:</th> <td><input type="text" name="account" /></td> </tr> <tr> <th>密码:</th> <td><input type="password" name="pass" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value=" 登 录 " /> </td> </tr> </table> </form> </fieldset> </body> </html>
/CHATROOM/WebContent/sendMessage.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="doSend.jsp" method="post"> <textarea rows="6" cols="60" name="message"></textarea> <br/> <span> <input type="submit" value=" 发 送 " /> </span> </form> </body> </html>
/CHATROOM/WebContent/showMessageList.jsp:
<%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <meta http-equiv="refresh" content="1"> <% // 获取存储在application当中已经发送过消息集合 List<String> messages = (List<String>)application.getAttribute("msglist"); if(messages != null && messages.size()>0) { for(String message : messages) { out.print(message); } } %>