/*******************************************************************************
*                                                                              *
*  File:        Servlet_00.java                         Revision:  1.0         *
*                                                                              *
*  Purpose:     a first Java Servlet                                           *
*                                                                              *
*  Creation:    07.09.2000                     Last Modification:  09.09.2000  *
*                                                                              *
*  Platform:    IBM-compatible PC running Windows 98SE                         *
*                                                                              *
*  Environment: Java 1.3, Gefion Software LiteWebServer 2.2.1                  *
*                                                                              *
*  Author:      Andreas Rozek           Phone:  ++49 (711) 6770682             *
*               Kirschblütenweg 15      Fax:    -                              *
*             D-70569 Stuttgart         EMail:  Andreas.Rozek@T-Online.De      *
*               Germany                                                        *
*                                                                              *
*  URL:         http://Home.T-Online.De/home/Andreas.Rozek                     *
*                                                                              *
*  Copyright:   this software is published under the "GNU General Public Li-   *
*               cense" (see "http://www.gnu.org/copyleft/gpl.html" for addi-   *
*               tional information)                                            *
*                                                                              *
*  Comments:    the paths in this servlet assume that the LiteWebServer is     *
*               still in its default configuration                             *
*                                                                              *
*               Warning: for the sake of simplicity, this servlet is not able  *
*               to handle several requests concurrently                        *
*                                                                              *
*******************************************************************************/

import java.io.*;                            // Java file handling and stream IO
import javax.servlet.*;                        // servlet implementation classes
import javax.servlet.http.*;                             // servlet HTTP support

public class Servlet_00 extends HttpServlet {

/*******************************************************************************
*                                                                              *
*                         Non-Public Instance Variables                        *
*                                                                              *
*******************************************************************************/

  PrintWriter OutStream;         // is used to construct this servlet's response
  boolean     oddTableRow;                       // a simple table row "counter"

/*******************************************************************************
*                                                                              *
*                                 Constructor                                  *
*                                                                              *
*******************************************************************************/

  public Servlet_00 () {
    super();                                              // just to be complete
System.out.println("Servlet_00: initializing");
  };

/*******************************************************************************
*                                                                              *
*                           Public Instance Methods                            *
*                                                                              *
*******************************************************************************/

/*******************************************************************************
*                                                                              *
* doGet                                                  handles a GET request *
*                                                                              *
*******************************************************************************/

  public void doGet (HttpServletRequest Request, HttpServletResponse Response)
  throws ServletException, IOException {
System.out.println("Servlet_00: starting to process the request");
System.out.println("Servlet_00: using servlet object " + this);
    try {
      Response.setContentType("text/html");

      OutStream = Response.getWriter();
        OutStream.println("<!DocType HTML public \"-//W3C//DTD HTML 4.01//EN\"");
        OutStream.println(" \"http://www.w3.org/tr/html4/loose.dtd\">");
        OutStream.println("<html>");
        OutStream.println("  <head>");
        OutStream.println("    <title>Servlet_00 - a first Java Servlet</title>");
        OutStream.println("  </head>");
        OutStream.println();
        OutStream.println("  <body background=\"/Paper.jpg\">");
        OutStream.println("    <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=600>");
        OutStream.println("      <tr valign=\"bottom\">");
        OutStream.println("        <td align=left><B>Servlet_00 - a first Java Servlet</B></td>");
        OutStream.println("        <td align=right></td>");
        OutStream.println("      </tr>");
        OutStream.println();
        OutStream.println("      <tr valign=top>");
        OutStream.println("        <td height=10 colspan=2><img src=\"/Line_600.gif\" width=\"600\" height=\"2\"></td>");
        OutStream.println("      </tr>");
        OutStream.println("    </table>");

        OutStream.println("    <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=600>");
        OutStream.println("      <tr valign=\"top\">");
        OutStream.println("        <td align=left>");
        OutStream.println("          <h3>CGI-Variables</h3>");
        OutStream.println("          <p align=justify>");
        OutStream.println("            The following table shows the settings of several CGI-Variables:");
        OutStream.println("          <p align=justify>");
        OutStream.println("            <table border=\"0\" cellspacing=\"0\" cellpadding=\"2\" width=100%>");
          oddTableRow = true;

          showVariable("AUTH_TYPE",       Request.getAuthType());
          showVariable("CONTENT_LENGTH",  String.valueOf(Request.getContentLength()));
          showVariable("CONTENT_TYPE",    Request.getContentType());
          showVariable("DOCUMENT_ROOT",   getServletContext().getRealPath("/"));
          showVariable("PATH_INFO",       Request.getPathInfo());
          showVariable("PATH_TRANSLATED", Request.getPathTranslated());
          showVariable("QUERY_STRING",    Request.getQueryString());
          showVariable("REMOTE_ADDR",     Request.getRemoteAddr());
//        showVariable("REMOTE_HOST",     Request.getRemoteHost()); // warning! takes long to complete!
          showVariable("REMOTE_USER",     Request.getRemoteUser());
          showVariable("REQUEST_METHOD",  Request.getMethod());
          showVariable("SCRIPT_NAME",     Request.getServletPath());
          showVariable("SERVER_NAME",     Request.getServerName());
          showVariable("SERVER_PORT",     String.valueOf(Request.getServerPort()));
          showVariable("SERVER_PROTOCOL", Request.getProtocol());
          showVariable("SERVER_SOFTWARE", getServletContext().getServerInfo());
        OutStream.println("            </table>");
        OutStream.println("        </td>");
        OutStream.println("      </tr>");
        OutStream.println("    </table>");

        OutStream.println("    <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=600>");
        OutStream.println("      <tr valign=\"bottom\">");
        OutStream.println("        <td height=10 colspan=2><img src=\"/Line_600.gif\" width=\"600\" height=\"2\"></td>");
        OutStream.println("      </tr>");
        OutStream.println();
        OutStream.println("      <tr valign=top>");
        OutStream.println("        <td align=left><I><A href=\"mailto:Andreas.Rozek@T-Online.De\">Andreas Rozek</A></I></td>");
        OutStream.println("        <td align=right></td>");
        OutStream.println("      </tr>");
        OutStream.println("    </table>");
        OutStream.println("  </body>");
        OutStream.println("</html>");
      OutStream.close();
    } catch (IOException Signal) {
      log(
        "caught IOException " + Signal.getClass().getName() + " while constructing " +
        "the response (reason: \"" + Signal.getMessage() + "\")"
      );
    };
System.out.println("Servlet_00: finished processing the request");
  };

/*******************************************************************************
*                                                                              *
* doPost                                                handles a POST request *
*                                                                              *
*******************************************************************************/

  public void doPost (HttpServletRequest Request, HttpServletResponse Response)
  throws ServletException, IOException {
    doGet(Request, Response);
  };

/*******************************************************************************
*                                                                              *
* getLastModified                 yields the timestamp of the requested object *
*                                                                              *
*******************************************************************************/

  public long getLastModified (HttpServletRequest Request) {
    return -1L;
  };

/*******************************************************************************
*                                                                              *
* showVariable                      shows the contents of a given CGI variable *
*                                                                              *
*******************************************************************************/

  public void showVariable (String Name, String Value) {
System.out.println("Servlet_00: handling " + Name + "...");
    OutStream.println("              <tr valign=top bgcolor=" + (oddTableRow ? "#CCFFCC" : "white") + ">");
    OutStream.println("                <td align=left><img src=\"/PaperHole.gif\"></td>");
    OutStream.println("                <td align=right><b><tt>" + Name + "</tt></b>:&nbsp;</td>");
    OutStream.print  ("                <td align=left>");
      if (Value == null) {
        OutStream.print("<i>(not available)</i>");
      } else {
        OutStream.print("\"" + Value + "\"");
      };
    OutStream.println("</td>");
    OutStream.println("                <td align=right><img src=\"/PaperHole.gif\"></td>");
    OutStream.println("              </tr>");

    oddTableRow = !oddTableRow;
  };
};

