FileDocCategorySizeDatePackage
ChatDispatch.javaAPI DocExample3102Tue Jan 25 10:45:14 GMT 2000None

ChatDispatch

public class ChatDispatch extends HttpServlet

Fields Summary
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    res.setContentType("text/html");

    if (!req.getParameterNames().hasMoreElements()) {
      // There were no request parameters.  Print a welcome page.
      printWelcomePage(req, res);
    }
    else {
      // There was at least one request parameter.
      // Print a page containing the applet.
      printAppletPage(req, res);
    }
  
private voidprintAppletPage(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    PrintWriter out = res.getWriter();

    out.println("<HTML>");
    out.println("<HEAD><TITLE>An Absurdly Simple Chat</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<H1>An Absurdly Simple Chat</H1>");

    String method = req.getParameter("method");
    String user = req.getRemoteUser();
    String applet = null;

    if ("http".equals(method)) {
      applet = "HttpChatApplet";
    }
    else if ("socket".equals(method)) {
      applet = "SocketChatApplet";
    }
    else if ("rmi".equals(method)) {
      applet = "RMIChatApplet";
    }
    else {
      // No method given, or an invalid method given.
      // Explain to the user what we expect.
      out.println("Sorry, this servlet requires a <TT>method</TT> " + 
                  "parameter with one of these values: " + 
                  "http, socket, rmi"); 
      return;
    }

    // Print the HTML code to generate the applet.
    // Choose the applet code based on the method parameter.
    // Provide a user parameter if we know the remote user.
    out.println("<APPLET CODE=" + applet + " CODEBASE=/ " +
                "WIDTH=500 HEIGHT=170>");
    if (user != null)
      out.println("<PARAM NAME=user VALUE=\"" + user + "\">");
    out.println("</APPLET>");

    out.println("</BODY></HTML>");
  
private voidprintWelcomePage(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    PrintWriter out = res.getWriter();
    String me = req.getServletPath();

    out.println("<HTML>");
    out.println("<HEAD><TITLE>");
    out.println("Welcome to an Absurdly Simple Chat");
    out.println("</TITLE></HEAD>");
    out.println();
    out.println("<BODY>");
    out.println("<H1>Welcome to an Absurdly Simple Chat</H1>");
    out.println();
    out.println("Would you like to communicate via:");
    out.println("<UL>");
    out.println("  <LI><A HREF=\"" + me + "?method=http\">http</A>");
    out.println("  <LI><A HREF=\"" + me + "?method=socket\">socket</A>");
    out.println("  <LI><A HREF=\"" + me + "?method=rmi\">rmi</A>");
    out.println("</UL>");
    out.println("</BODY></HTML>");