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+ajax简单聊天室

 [Y2T22]首页 - 讨论区 - 我写了一个jsp+ajax简单聊天室 返 回|发 帖回 帖 上一篇下一篇 >   ***中文问题,有兴趣可以解决一下***************/  /*...

java websocket 建立一个聊天室

在新建一个聊天室之前我们首先要了解什么是websock以及websocket api的一些常用知识 一、什么是WebSocket ? 在HTML5规范中,我最喜欢的Web技术就是正迅速变得流行的W...

AJAX_JSP 聊天室1_0 源码

  • 2007年07月27日 13:51
  • 12KB
  • 下载

JSP在线聊天室——实验五之软件运行界面2

管理界面                 输入用户名搜索聊天记录         输入:ninarming---à搜索         返回的是管理界面         修改公告栏 ...

jsp,ajax实现的聊天室

  • 2012年02月20日 10:51
  • 2.45MB
  • 下载

jsp聊天室AJAX

  • 2009年04月25日 23:56
  • 232KB
  • 下载

JSP在线聊天室——软工课程心得与体会

这门课让我受益颇丰!很多知识都没接触过,真的是长见识了,这门课学的不仅仅是理论的,相比于其他课,这课的实战性更强,更有利于对自己所学的知识加强理解,而且老师还告诉了学习方法,虽然一时半会不可能像老师说...

Ajax+jsp做的聊天室

  • 2009年11月03日 11:54
  • 4KB
  • 下载

jsp+ajax实现简单聊天室

  • 2008年03月23日 17:50
  • 13KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ajax聊天室(JSP版)4
举报原因:
原因补充:

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