FileDocCategorySizeDatePackage
ChatServlet.javaAPI DocApache Tomcat 6.0.149606Fri Jul 20 04:20:36 BST 2007chat

ChatServlet

public class ChatServlet extends HttpServlet implements org.apache.catalina.CometProcessor
Helper class to implement Comet functionality.

Fields Summary
protected ArrayList
connections
protected MessageSender
messageSender
Constructors Summary
Methods Summary
protected voidbegin(org.apache.catalina.CometEvent event, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        log("Begin for session: " + request.getSession(true).getId());
        
        PrintWriter writer = response.getWriter();
        writer.println("<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">");
        writer.println("<html><head><title>JSP Chat</title></head><body bgcolor=\"#FFFFFF\">");
        writer.flush();

        synchronized(connections) {
            connections.add(response);
        }
    
public voiddestroy()

        connections.clear();
        messageSender.stop();
        messageSender = null;
    
protected voidend(org.apache.catalina.CometEvent event, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        log("End for session: " + request.getSession(true).getId());
        synchronized(connections) {
            connections.remove(response);
        }
        
        PrintWriter writer = response.getWriter();
        writer.println("</body></html>");
        
        event.close();
        
    
protected voiderror(org.apache.catalina.CometEvent event, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        log("Error for session: " + request.getSession(true).getId());
        synchronized(connections) {
            connections.remove(response);
        }
        event.close();
    
public voidevent(org.apache.catalina.CometEvent event)
Process the given Comet event.

param
event The Comet event that will be processed
throws
IOException
throws
ServletException


        // Note: There should really be two servlets in this example, to avoid
        // mixing Comet stuff with regular connection processing
        HttpServletRequest request = event.getHttpServletRequest();
        HttpServletResponse response = event.getHttpServletResponse();
        
        if (event.getEventType() == CometEvent.EventType.BEGIN) {
            String action = request.getParameter("action");
            if (action != null) {
                if ("login".equals(action)) {
                    String nickname = request.getParameter("nickname");
                    request.getSession(true).setAttribute("nickname", nickname);
                    response.sendRedirect("post.jsp");
                    event.close();
                    return;
                } else {
                    String nickname = (String) request.getSession(true).getAttribute("nickname");
                    String message = request.getParameter("message");
                    messageSender.send(nickname, message);
                    response.sendRedirect("post.jsp");
                    event.close();
                    return;
                }
            } else {
                if (request.getSession(true).getAttribute("nickname") == null) {
                    // Redirect to "login"
                    log("Redirect to login for session: " + request.getSession(true).getId());
                    response.sendRedirect("login.jsp");
                    event.close();
                    return;
                }
            }
            begin(event, request, response);
        } else if (event.getEventType() == CometEvent.EventType.ERROR) {
            error(event, request, response);
        } else if (event.getEventType() == CometEvent.EventType.END) {
            end(event, request, response);
        } else if (event.getEventType() == CometEvent.EventType.READ) {
            read(event, request, response);
        }
    
public voidinit()

    
         
        messageSender = new MessageSender();
        Thread messageSenderThread = 
            new Thread(messageSender, "MessageSender[" + getServletContext().getContextPath() + "]");
        messageSenderThread.setDaemon(true);
        messageSenderThread.start();
    
protected voidread(org.apache.catalina.CometEvent event, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        InputStream is = request.getInputStream();
        byte[] buf = new byte[512];
        while (is.available() > 0) {
            log("Available: " + is.available());
            int n = is.read(buf);
            if (n > 0) {
                log("Read " + n + " bytes: " + new String(buf, 0, n) 
                        + " for session: " + request.getSession(true).getId());
            } else if (n < 0) {
                log("End of file: " + n);
                end(event, request, response);
                return;
            }
        }
    
protected voidservice(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        // Compatibility method: equivalent method using the regular connection model
        PrintWriter writer = response.getWriter();
        writer.println("<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">");
        writer.println("<html><head><title>JSP Chat</title></head><body bgcolor=\"#FFFFFF\">");
        writer.println("Chat example only supports Comet processing");
        writer.println("</body></html>");