ajax聊天室(JSP版)4

原创 2007年09月17日 23:24:00

Servlet类真的没什么好说的。

先写个BaseServlet基类,以后的几个Servlet类就直接继承它,而不是HttpServlet。


package org.jvk.chatroom.servlet;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
public abstract class BaseServlet extends HttpServlet {
 
    public final static String USER = "user";
 
    private static final longserialVersionUID = 520131401655L;
 
    protected Log log = LogFactory.getLog(this.getClass());
 
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       execute(request, response);
    }
 
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       execute(request, response);
    }
 
    /**
     *由子类实现,处理GETPOST请求
     *
     *@paramrequest
     *@paramresponse
     *@throwsServletException
     *@throwsIOException
     */
    publicab stract void execute(HttpServletRequest request,
           HttpServletResponse response) throws ServletException, IOException;
 
    protected final String getValue(HttpServletRequest request, String param) {
       String str = request.getParameter(param);
       if (str == null)
           return"";
       return str;
    }
 
    private static byte[] lock = new byte[0];
 
    protected final void setNoCache(HttpServletRequest request,
           HttpServletResponse response) {
       synchronized (lock) {
           if (request.getProtocol().compareTo("HTTP/1.0") == 0)
              response.setHeader("Prama", "no-cache");
           else if (request.getProtocol().compareTo("HTTP/1.1") == 0)
              response.setHeader("Cache-Control", "no-cache");
           response.setDateHeader("Expires", 0);
       }
    }
}

 
package org.jvk.chatroom.servlet;
 
import java.io.IOException;
import java.util.Collection;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.jvk.chatroom.bean.Room;
import org.jvk.chatroom.bean.User;
import org.jvk.chatroom.service.ChatroomManager;
 
public class Login extends BaseServlet {
 
    private static final longserialVersionUID = 5201314016000L;
 
    // 设置session生命周期为1分钟
    private static intsessionTimeOut = 60 * 1;
 
    private static final String ROOM_PAGE = "pages/chatroom.jsp";
 
    private static final String LOGIN_PAGE = "pages/login.jsp";
 
    private staticf inal String this_PAGE = "login";
 
    private static final String ERROR_PAGE = "pages/error.jsp";
 
    public void execute(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       String userName = getValue(request, "userName");
       String roomName = getValue(request, "roomName");
       if (userName.length() < 1 || roomName.length() < 1) {
           Collection<Room> rooms = ChatroomManager.newInstance().getRooms()
                  .values();
           request.setAttribute("rooms", rooms);
           request.getRequestDispatcher(LOGIN_PAGE).forward(request, response);
       } else if (ChatroomManager.newInstance().exists(roomName, userName)) {
           log.info("该用户已存在,请使用其他字符");
           request.setAttribute("returnURL", this_PAGE);
           request.setAttribute("errorMessage", "该用户已存在,请使用其他字符");
           request.getRequestDispatcher(ERROR_PAGE).forward(request, response);
       } else {
           HttpSession session = request.getSession(true);
           User user = new User(userName, ChatroomManager.newInstance()
                  .getRoom(roomName));
           session.setMaxInactiveInterval(sessionTimeOut);
           user.setLastGetMessage(510);
           user.setLastGetUserList(520);
           session.setAttribute(USER, user);
           session.setAttribute("room", ChatroomManager.newInstance().getRoom(
                  roomName));
           response.sendRedirect(ROOM_PAGE);
       }
    }
 
}

package org.jvk.chatroom.servlet;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.jvk.chatroom.bean.Message;
import org.jvk.chatroom.bean.User;
import org.jvk.chatroom.service.ChatroomManager;
 
 
public class SendMessage extends BaseServlet {
    private static final longserialVersionUID = 520131401655L;
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException,
           IOException {
       HttpSession session = request.getSession(false);
       if (session == null){
           response.getWriter().write("");
           return;
       }
       synchronized (session) {
           User user = (User) session.getAttribute(USER);
           Message message = new Message();
           String to = request.getParameter("to");
           if ("all".equals(to))
              to = null;
           String content = request.getParameter("content");
           message.setContent(content);
           message.setFrom(user.getName());
           message.setTo(to);
           message.setDate(System.currentTimeMillis());
           ChatroomManager.newInstance().addMessageToRoom(message, user.getRoom());
           //假装响应下
           response.getWriter().write("");
       }
    }
}
 

 
package org.jvk.chatroom.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.jvk.chatroom.bean.Message;
import org.jvk.chatroom.bean.User;
import org.jvk.chatroom.service.ChatroomManager;
 
public class GetMessage extends BaseServlet {
 
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException,
           IOException {
       HttpSession session = request.getSession(false);
       if (session == null)
           return;
       synchronized (session) {
           User user = (User) session.getAttribute(USER);
           List<Message> messages = ChatroomManager.newInstance().getMessagesFromRoom(user);
           StringBuffer xml = new StringBuffer(4096);
           xml.append("<messages>");
           for (Message message : messages) {
              xml.append("<message>");
              xml.append("<from>" + message.getFrom() + "</from>");
              xml.append("<date>" + new SimpleDateFormat().format(new Date(message.getDate())) + "</date>");
              xml.append("<content>" + message.getContent() + "</content>");
              xml.append("</message>");
           }
           xml.append("</messages>");
           System.out.println("xml="+xml);
           setNoCache(request, response);
           response.setContentType("text/xml; charset=UTF-8");
           PrintWriter out = response.getWriter();
           out.print(xml);
           out.flush();
       }
 
    }
 
}
 

 
package org.jvk.chatroom.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.jvk.chatroom.bean.User;
import org.jvk.chatroom.service.ChatroomManager;
 
public class GetUserList extends BaseServlet {
 
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       HttpSession session = request.getSession(false);
       if (session == null)
           return;
       synchronized (session) {
           User user = (User) session.getAttribute(USER);
           ChatroomManager dao = ChatroomManager.newInstance();
           List<User> list = dao.getUsersFromRoom(user);
          
           StringBuffer buffer = new StringBuffer(1024);
           buffer.append("<users>");
           for (User tempUser : list) {
              buffer.append("<user name=/"" + tempUser.getName() + "/"/>");
           }
           buffer.append("</users>");
           log.info("users=/n" + buffer);
           setNoCache(request,response);
           response.setContentType("text/xml; charset=UTF-8");
           PrintWriter out = response.getWriter();
           out.print(buffer);
           out.flush();
       }
 
    }
}

没有了,就这几个类。 
 

一对一网页聊天 jsp+js+ajax+servlet+tomcat+mysql

初学javaweb,很想做一个网页版的一对一聊天系统,但是在网上查了一下发现并没有相应的资源,网页聊天室倒是有很多,一个原因是B/S与C/S相比不适合做实时通讯,另外可能是一对一聊天做起来比较难(我一...
  • YaYhui
  • YaYhui
  • 2016年04月17日 00:22
  • 1382

ajax聊天室(JSP版)1

初学JSP编程,一般喜欢做做留言板、聊天室、购物网站或论坛等。下面我详细说说如何实现一个基于WEB的无刷新聊天室。注:参考了Struts版的 ajaxchat。平台或技术:Windows XP,JSP...
  • Windy83
  • Windy83
  • 2007年09月13日 01:34
  • 3895

我写了一个jsp+ajax简单聊天室

 [Y2T22]首页 - 讨论区 - 我写了一个jsp+ajax简单聊天室 返 回|发 帖回 帖 上一篇下一篇 >   ***中文问题,有兴趣可以解决一下***************/  /*...
  • wang_zilu
  • wang_zilu
  • 2009年02月20日 10:28
  • 1553

JSP无刷新聊天室

login.html 登录聊天室 登录聊天室
  • xue1225go
  • xue1225go
  • 2012年03月15日 09:59
  • 5950

Ajax jQuery Web聊天室

实现界面: 1.进入聊天室前必须进行登录,保存用户基本信息,为了显示在线人数 2.登录聊天时,调用Ajax的全局函数$.ajax(),将获取的用户名和密码数据向处理逻辑发送,使用AjaxStart...
  • Ace_0811
  • Ace_0811
  • 2015年03月09日 17:41
  • 1064

基于jQuery的Ajax聊天室程序

一个基于Ajax无需刷新的聊天室程序,实现多用户在网页上聊天。 参考:《锋利的JQuery》 源码下载:点击打开链接...
  • zhu1988renhui
  • zhu1988renhui
  • 2012年10月06日 15:06
  • 4283

ajax聊天室(JSP版)3

有了以上三个javabean,只是==有了三个数据表(事实上,如果要保存聊天信息在数据库中,建立表进行插入删除更新即可),还要有掌控全局的ChatroomManager类。下面先给出完整代码。pack...
  • Windy83
  • Windy83
  • 2007年09月15日 20:40
  • 2574

ajax聊天室(JSP版)6

现在剩下JSP页面的HTML代码和JS代码了。直接贴代码,哪天用到了可以回头看看。chat.js文件,用到了Prototype.js/** *ajax聊天室js代码 *聊天室version1.0 *2...
  • Windy83
  • Windy83
  • 2007年09月20日 22:24
  • 2975

ajax聊天室(JSP版)5

用来的Java类,除了上述几个外,就剩下以下几个了。两个过滤器类,一个用来过滤字符,一个用来过滤未登录的用户。package org.jvk.chatroom.filter; import java....
  • Windy83
  • Windy83
  • 2007年09月19日 21:18
  • 5563

ajax聊天室(JSP版)2

这一回主要谈谈上一篇文章给出的三个Java类的代码。消息类(Message)的代码很简单,只是普通的Javabean。唯一值得注意的是消息发送时间并没有使用Java的Date类型,而用了long数据类...
  • Windy83
  • Windy83
  • 2007年09月13日 21:56
  • 2224
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ajax聊天室(JSP版)4
举报原因:
原因补充:

(最多只允许输入30个字)