FileDocCategorySizeDatePackage
CometServlet.javaAPI DocGlassfish v2 API8000Fri May 04 22:36:56 BST 2007com.sun.grizzly.comet

CometServlet

public class CometServlet extends HttpServlet
Simple CometChat.
author
Jeanfrancois Arcand

Fields Summary
private String
contextPath
static int
firstServlet
Constructors Summary
public CometServlet()

     
      
    
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        doPost(request,response);
    
public voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        
            String action = request.getParameter("action");
            CometEngine cometEngine = CometEngine.getEngine();
            CometContext cometContext = cometEngine.getCometContext(contextPath);

            if (action != null) {              
                if ("login".equals(action)) {
                    String username = request.getParameter("username");
                    request.getSession(true).setAttribute("username", username);                    
                             
                    if (firstServlet != -1){
                         cometContext.notify("User " + username 
                          + " from " + request.getRemoteAddr()
                          + " is joinning the chat.<br/>",CometEvent.NOTIFY,
                                 firstServlet); 
                    }
                    
                    response.sendRedirect("chat.jsp");
                    return;
                } else if ("post".equals(action)){
                    String username = (String) request.getSession(true)
                        .getAttribute("username");
                    String message = request.getParameter("message");
                    cometContext.notify("[ " + username + " ]  " 
                            + message + "<br/>");
                    response.sendRedirect("post.jsp");   
                    return;
                } else if ("openchat".equals(action)) {
                    response.setContentType("text/html");
                    String username = (String) request.getSession(true)
                        .getAttribute("username");
                    response.getWriter().println("<h2>Welcome " 
                            + username + " </h2>");
                    
                    CometRequestHandler handler = new CometRequestHandler();
                    handler.clientIP = request.getRemoteAddr();
                    handler.attach(response.getWriter());
                    cometContext.addCometHandler(handler);
                    return;
                } else if ("openchat_admin".equals(action)) {
                    response.setContentType("text/html");
                    CometRequestHandler handler = new CometRequestHandler();
                    handler.attach(response.getWriter());
                    if (firstServlet == -1){
                        handler.clientIP = request.getRemoteAddr();
                        firstServlet = cometContext.addCometHandler(handler);
                        response.getWriter().println("<h2>Master Chat Window</h2>");
                    } else {
                        response.getWriter()
                            .println("<h2>Moderator already logged</h2>");
                    }
                    return;
                }
                
            } 
    
public voidinit(javax.servlet.ServletConfig config)

 
        super.init(config);
        contextPath = config.getServletContext().getContextPath() + "/chat";
        CometEngine cometEngine = CometEngine.getEngine();
        CometContext context = cometEngine.register(contextPath);    
        context.setExpirationDelay(20 * 1000);