基于Servlet和Properties的JSP聊天室案例
package com.demo.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Properties;
public class ChatService {
// 使用单例模式来设计CharService
private static ChatService cs;
// 使用Properties对象保存系统的所有用户
private Properties userList;
// 使用LinkedList对象保存聊天信息
private LinkedList<String> chatMsg;
// 构造器私有
private ChatService() {
}
// 通过静态方法返回唯一的ChatService对象
public static ChatService instance() {
if (cs == null) {
cs = new ChatService();
}
return cs;
}
// 验证用户登陆
public boolean validLogin(String user, String pass)
throws IOException {
String loadPass = loadUser().getProperty(user);
if (loadPass != null && loadPass.equals(pass)) {
return true;
}
return false;
}
//用户注册
public boolean addUser(String name,String pass)
throws Exception{
//当userList为null,初始化userList对象
if(userList==null){
userList=loadUser();
}
//如果用户名存在
if(userList.containsKey(name)){
throw new Exception("用户名已存在!");
}
userList.setProperty(name, pass);
saveUserList();
return true;
}
//获取系统中所用的聊天信息
public String getMsg(){
//如果ChatMsg对象为null,表面不曾开始聊天
if(chatMsg==null){
chatMsg=new LinkedList<String>();
return "";
}
StringBuilder resultStr=new StringBuilder("");
//将chatMsg中所有聊天信息拼接起来
for(String tmp:chatMsg){
resultStr.append(tmp+"\n");
}
return resultStr.toString();
}
//用户发言,添加聊天信息
public void addMsg(String user,String msg){
//如果chatMsg对象为null,初始化chatMsg对象
if(chatMsg==null){
chatMsg=new LinkedList<String>();
}
//chatMsg超过40条时,将前面的聊天信息删除
if(chatMsg.size()>40){
chatMsg.removeFirst();
}
chatMsg.add(user+"说:"+msg);
}
/* 下面是系统的工具方法 */
// 读取系统用户信息
private Properties loadUser() throws IOException {
if (userList == null) {
File f = new File("userFile.properties");
if (!f.exists()) {
f.createNewFile();
}
userList = new Properties();
userList.load(new FileInputStream(f));
}
return userList;
}
// 保存系统所用用户
private boolean saveUserList() throws IOException {
if (userList == null) {
return false;
}
// 将userList信息保存到Properties文件中
userList.store(new FileOutputStream
("userFile.properties"),
"Users Info List");
return true;
}
}
package com.demo.service;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ChatServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("GBK");
String msg=request.getParameter("chatMsg");
if(msg!=null&!msg.equals("")){
String user=(String)request.getSession(true)
.getAttribute("user");
ChatService.instance().addMsg(user, msg);
}
request.setAttribute("msg",
ChatService.instance().getMsg());
forward("/chat.jsp",request,response);
// super.service(request, response);
}
private void forward(String url, HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
RequestDispatcher rd=request.getRequestDispatcher(url);
rd.forward(request, response);
}
}
<%@ page contentType="text/html;charset=GBK" errorPage="error.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>聊天页面</title>
</head>
<body>
<table width="780" border="1" align="center">
<tr>
<td>
<p align="center">
<textarea name="textarea" cols="100" rows="30"
readonly="readonly">${requestScope.msg}</textarea>
</p>
<form name="form1" method="post" action="chat.do">
<div align="center">
<input name="chatMsg" type="text" size="90"
onclick="document.form1.submit";/>
<input type="submit" name="Submit" value="发送" />
</div>
</form>
</td>
</tr>
</table>
<script>
var input = document.getElementById("chatMsg");
input.focus();
function check() {
if (input.value == null || input.value == "") {
alert("请输入聊天信息,不要发送空信息!");
return false;
}
}
function submitChat() {
document.getElementById("form1").onsubmit();
}
document.getElementById("form1").onsubmit = check;
</script>
</body>
</html>