FileDocCategorySizeDatePackage
ChatPage.javaAPI DocExample4508Tue Jan 25 10:45:14 GMT 2000None

ChatPage

public class ChatPage extends HttpServlet implements Runnable

Fields Summary
static final int
MESSAGE_ARCHIVE_SIZE
ChatServlet
chat
String[]
messages
int
messageIndex
Thread
update
Constructors Summary
Methods Summary
public voiddestroy()

    if (update != null)
      update.stop();
  
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    // Turn off caching, so the latest messages are always displayed.
    // (Works around a Netscape problem.)
    res.setHeader("Pragma", "no-cache");

    // For our first request, "chat" is null and needs to use
    // ServletUtils.getServlet() to get the ChatServlet instance.
    // Then we need to start another thread to listen for chat's
    // new messages.
    if (chat == null) {
      chat = (ChatServlet)ServletUtils.getServlet(
                 "ChatServlet", req, getServletContext());
      if (chat != null) {
        update = new Thread(this);
        update.start();
      }
    }

    // Print a pretty header.
    out.println("<HTML>");
    out.println("<HEAD><TITLE>ChatPage</TITLE></HEAD>");
    out.println("<BODY><CENTER><H1>Welcome to ChatPage!</H1></CENTER>");

    // Print the message archive, oldest first.
    // Synchronized so it doesn't change while we're printing it.
    synchronized (this) {
      out.println("<FONT SIZE=4>Recent messages:</FONT><P>");
      int i = messageIndex;
      do {
        String message = messages[i];
        if (message != null) out.println(message + "<P>");
        i = (i + 1) % MESSAGE_ARCHIVE_SIZE;
      } while (i != messageIndex);
    }

    // Print a button that gets new messages.
    out.println("<FORM METHOD=GET>");
    out.println("<INPUT TYPE=SUBMIT VALUE=\"Get New Messages\">");
    out.println("</FORM>");

    // Print a form where the reader can submit a new message.
    out.println("<HR>");
    out.println("<FORM METHOD=POST>");
    out.println("<FONT SIZE=4>Submit a message:</FONT>");
    out.println("<INPUT TYPE=TEXT NAME=message>");
    out.println("</FORM>");

    // Print a pretty footer.
    out.println("<HR>");
    out.println("<CENTER><FONT SIZE=2><B>");
    out.println("Special thanks to ChatServlet for acting as our back-end");
    out.println("</B></FONT></CENTER>");
    out.println("</BODY></HTML>");
  
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    // If our first request happens to be a POST, we need to set "chat"
    // and start our update thread just like we do for a GET request.
    if (chat == null) {
      chat = (ChatServlet)ServletUtils.getServlet(
                 "ChatServlet", req, getServletContext());
      if (chat != null) {
        update = new Thread(this);
        update.start();
        Thread.currentThread().yield();  // let the run() method start
      }
    }

    // Get the client's username.  It's non-null only if ChatPage is
    // protected by client authentication.
    String user = req.getRemoteUser();
    if (user == null) user = "anonymous";

    // Get and broadcast the message.
    String message = req.getParameter("message");
    if (message != null && chat != null) {
      chat.broadcastMessage(user + ": " + message);
      Thread.currentThread().yield();  // let the message be broadcast
    }

    // Have doGet() print the updated message archive and the form.
    doGet(req, res);
  
public java.lang.StringgetServletInfo()

    return "An HTML chat server front end, reusing ChatServlet";
  
public voidrun()

  // thread to update new messages

  // Gets new messages from the chat servlet and inserts them in 
  // the messages circular array.
     
    while (true) {
      String message = chat.getNextMessage();
      synchronized (this) {
        messages[messageIndex] = message;
        messageIndex = (messageIndex + 1) % MESSAGE_ARCHIVE_SIZE;
      }
    }