Login.jsp文件: <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <!doctype html> <html> <head> <!--声明当前页面的编码集:charset=gbk,gb2312(中文编码),utf-8国际编码--> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <!--当前页面的三要素--> <title>多人聊天室 - 登录</title> <meta name="Keywords" content="关键词,关键词"> <meta name="description" content=""> <!--css,js--> <style type="text/css"> *{margin:0;padding:0;} .text{200px;height:26px;line-height:26px;} </style> </head> <body> <h1>聊天室 - 登录</h1> <form action="loadMes.jsp?action=login" method="post"> 用户名:<input type="text" name="username" class="text"/> <br /> 密 码:<input type="password" name="password" class="text"/><br /><br /> <input type="submit" value="登 录"/> </form> </body> </html> //////////////////////////////////////////////////////////// loadMes.jsp文件里: <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <jsp:useBean id="msgs" class="java.util.HashMap" scope="application"/> <% request.setCharacterEncoding("gbk"); String action = request.getParameter("action"); // 登录页面 if("login".equals(action)){ String username = request.getParameter("username"); String password = request.getParameter("password"); // 存储username到session对象里边 session.setAttribute("username", username); String msg = "欢迎<span style='color:red;font-size:16px;font-weight:bold;'>" + username + "</span> 加入聊天室! <br /><br />"; //System.out.println("开始:"+msg); msgs.put(username,msg); response.sendRedirect("main.jsp"); } // 消息展示页面 if("show".equals(action)){ // 展示聊天记录 String username = (String)session.getAttribute("username"); String msg = (String)msgs.get(username); //System.out.println("展示聊天记录:"+msg); out.println("loadData.innerHTML=""+msg+"";"); } // 消息输入 if("write".equals(action)){ String newMsg = request.getParameter("msg"); String userMsg = session.getAttribute("username") + " : " + newMsg; System.out.println("发送新消息:"+userMsg); // 发送消息时,将所有的聊天室里边聊天记录进行叠加 Iterator it = msgs.keySet().iterator(); String username = null; String msg = null; while(it.hasNext()){ username = (String)it.next(); // 用户名 msg = (String)msgs.get(username); // 获取全局变量中的消息 msg = msg +"<br /><br />"+ userMsg; msgs.put(username, msg); } response.sendRedirect("writerMes.jsp"); } %> ///////////////////////////////////////////////////////////////////// main.jsp文件: <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <!doctype html> <html> <head> <!--声明当前页面的编码集:charset=gbk,gb2312(中文编码),utf-8国际编码--> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <!--当前页面的三要素--> <title>多人聊天室 - 聊天室主窗体</title> <meta name="Keywords" content="关键词,关键词"> <meta name="description" content=""> <!--css,js--> <style type="text/css"> *{margin:0;padding:0;} </style> </head> <frameset rows="*,100" border="0" > <frame src="show.jsp"></frame> <frame src="writerMes.jsp"></frame> </frameset> </html> ////////////////////////////////////////////////////////////////// show.jsp文件里: <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <!doctype html> <html> <head> <!--声明当前页面的编码集:charset=gbk,gb2312(中文编码),utf-8国际编码--> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <!--当前页面的三要素--> <title>多人聊天室 - 消息展示页面</title> <meta name="Keywords" content="关键词,关键词"> <meta name="description" content=""> <meta http-equiv="refresh" content="1"/> <!--css,js--> <style type="text/css"> *{margin:0;padding:0;} body{background:#CC9999;} </style> <script type="text/javascript"> function LoadMsg(){ var url = "loadMes.jsp?action=show"; loadTime.src = url; } </script> <script type="text/javascript" id="loadTime"></script> </head> <body onLoad="javascript:LoadMsg();"> <span id="loadData">正在加载数据,请稍后喔 ..........</span> </body> </html> /////////////////////////////////////////////////////////////////// writeMesjsp文件里: <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <!doctype html> <html> <head> <!--声明当前页面的编码集:charset=gbk,gb2312(中文编码),utf-8国际编码--> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <!--当前页面的三要素--> <title>多人聊天室 - 消息的输入页面</title> <meta name="Keywords" content="关键词,关键词"> <meta name="description" content=""> <!--css,js--> <style type="text/css"> *{margin:0;padding:0;} body{background:#CC99FF;} .msg{height:26px;line-height:26px;} .sub{100px;height:30px;} </style> </head> <body> <form action="loadMes.jsp?action=write" method="post"> <%=session.getAttribute("username") %>:<input type="text" name="msg" size="60" class="msg" autofocus x-webkit-speech/> <input type="submit" value="发 送" class="sub"/> </form> </body> </html> /////////////////////////////////////////////////////////////////// index.jsp文件里: <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> </body> </html>