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;
}
}