Methods Summary |
---|
protected void | begin(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 void | destroy()
connections.clear();
messageSender.stop();
messageSender = null;
|
protected void | end(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 void | error(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 void | event(org.apache.catalina.CometEvent event)Process the given Comet event.
// 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 void | init()
messageSender = new MessageSender();
Thread messageSenderThread =
new Thread(messageSender, "MessageSender[" + getServletContext().getContextPath() + "]");
messageSenderThread.setDaemon(true);
messageSenderThread.start();
|
protected void | read(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 void | service(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>");
|