读书人

基于Servlet跟Properties的JSP聊天室案

发布时间: 2012-06-26 10:04:13 作者: rapoo

基于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 {// 使用单例模式来设计CharServiceprivate 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 {@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// TODO Auto-generated method stubrequest.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 stubRequestDispatcher 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>

读书人网 >JavaScript

热点推荐