/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); } } %>