import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; // File: EchoEnvironmentServlet.java // Listing 2 // /** * This servlet displays the CGI environment variables in a web page. * * @author Chad (shod) Darby, darby@j-nine.com * @version 3.41, 2 Nov 1997 * */ public class EchoEnvironmentServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { String replyPage = null; // Build the header for HTML page // replyPage = "Echo Environment Variables"; replyPage += ""; // Echo the environment variables // replyPage += "

Environment Variables

"; replyPage += "

Server Name: " + request.getServerName(); replyPage += "

Server Port: " + request.getServerPort(); replyPage += "

Server Protocol: " + request.getProtocol(); replyPage += "

Servlet Name: " + request.getServletPath(); replyPage += "

Method: " + request.getMethod(); replyPage += "

Path Info: " + request.getPathInfo(); replyPage += "

Path Translated: " + request.getPathTranslated(); replyPage += "

Query String: " + request.getQueryString(); replyPage += "

Request URI: " + request.getRequestURI(); replyPage += "

Remote Host: " + request.getRemoteHost(); replyPage += "

Remote Addr: " + request.getRemoteAddr(); replyPage += "

Remote User: " + request.getRemoteUser(); replyPage += "

Authorization Type: " + request.getAuthType(); replyPage += "

Content Type: " + request.getContentType(); replyPage += "

Content Length: " + request.getContentLength(); replyPage += "


"; // Just for kicks, let's loop thru and get the header info using an enumeration // replyPage += "

Request Header Information

"; Enumeration e = request.getHeaderNames(); String name; while (e.hasMoreElements()) { name = (String) e.nextElement(); replyPage += "

" + name + ": " + request.getHeader(name); } replyPage += "


"; // Build bottom of HTML page // replyPage += " "; // Set the content-type for the response header // note: no need for extra carriage returns! // response.setContentType("text/html"); // finally, send this formatted HTML page back to the client // PrintStream out = new PrintStream(response.getOutputStream()); out.println(replyPage); out.close(); } public String getServletInfo() { return "Echo Form Servlet, Chad (shod) Darby, 2 Nov 1997, www.j-nine.com"; } }