/** ==============================================================
  Projekt:        Java Servlet
  File:           HelloAkadia.java
  Beschreibung:   This servlet simply echos back the request line
                  and headers that were sent by the client, plus
                  any HTTPS information which is accessible.
  ================================================================              
*/


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloAkadia extends HttpServlet {

  public void doGet (HttpServletRequest req, HttpServletResponse res)
                       throws ServletException, IOException {
    PrintWriter out;

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

    out.println("<head>");
    out.println("<link rel=\"stylesheet\" href=\"https://www.akadia.com/docroot/css/website.css\" type=\"text/css\">");
    out.println("<title>Hello Akadia</title>");
    out.println("</head>");        
    out.println("<body bgcolor=\"#FFFFFF\" text=\"#000000\">");
    out.println("<h1>Welcome on Akadia's Site<BR></h1>");
    out.println("<h2>Apache JServ is working !<BR></h2>");
    out.println("<body>");
    out.println("<h2>Requested URL:</h2>");
    out.println("<pre>");
    out.println (HttpUtils.getRequestURL (req).toString ());
    out.println("</pre>");

    Enumeration enum = getServletConfig().getInitParameterNames();
    if (enum != null) {
      boolean first = true;
      while (enum.hasMoreElements()) {
        if (first) {
          out.println("<h2>Init Parameters</h2>");
          out.println("<pre>");
          first = false;
        }
        String param = (String) enum.nextElement();
        out.println(" "+param+": "+getInitParameter(param));
      }
      out.println("</pre>");
    }

    out.println("<h2>Request information from Akadia's Site:</h2>");
    out.println("<pre>");

    print(out, "Request method", req.getMethod());
    print(out, "Request URI", req.getRequestURI());
    print(out, "Request protocol", req.getProtocol());
    print(out, "Servlet path", req.getServletPath());
    print(out, "Path info", req.getPathInfo());
    print(out, "Path translated", req.getPathTranslated());
    print(out, "Query string", req.getQueryString());
    print(out, "Content length", req.getContentLength());
    print(out, "Content type", req.getContentType());
    print(out, "Server name", req.getServerName());
    print(out, "Server port", req.getServerPort());
    print(out, "Remote user", req.getRemoteUser());
    print(out, "Remote address", req.getRemoteAddr());
    print(out, "Remote host", req.getRemoteHost());
    print(out, "Authorization scheme", req.getAuthType());

    out.println("</pre>");
   
    Enumeration e = req.getHeaderNames();
    if (e.hasMoreElements()) {
      out.println("<h2>Request headers:</h2>");
      out.println("<pre>");
      while (e.hasMoreElements()) {
        String name = (String)e.nextElement();
        out.println(" " + name + ": " + req.getHeader(name));
      }
      out.println("</pre>");
    }

    e = req.getParameterNames();
    if (e.hasMoreElements()) {
      out.println("<h2>Servlet parameters (Single Value style):</h2>");
      out.println("<pre>");
      while (e.hasMoreElements()) {
        String name = (String)e.nextElement();
        out.println(" " + name + " = " + req.getParameter(name));
      }
      out.println("</pre>");
    }

    e = req.getParameterNames();
    if (e.hasMoreElements()) {
      out.println("<h2>Servlet parameters (Multiple Value style):</h2>");
      out.println("<pre>");
      while (e.hasMoreElements()) {
        String name = (String)e.nextElement();
        String vals[] = (String []) req.getParameterValues(name);
        if (vals != null) {
          out.print("<b> " + name + " = </b>");
          out.println(vals[0]);
          for (int i = 1; i<vals.length; i++)
          out.println("           " + vals[i]);
        }
        out.println("<p>");
      }
      out.println("</pre>");
    }
    out.println("<hr>");
    out.println("<pre>");
    out.println("<P><a href=\"/jsp/WEB-INF/classes/HelloAkadia.txt\">Show Java Servlet Source</a></P>");
    out.println("</pre>");
    out.println("</body>");
    out.println("</html>");
  }

  private void print (PrintWriter out, String name, String value) {
    out.print(" " + name + ": ");
    out.println(value == null ? "&lt;none&gt;" : value);
  }

  private void print (PrintWriter out, String name, int value) {
    out.print(" " + name + ": ");
    if (value == -1) {
      out.println("&lt;none&gt;");
    } else {
      out.println(value);
    }
  }
}