 package coreservlets;
package coreservlets; 

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


 /**//** Creates a table showing the current value of each
/**//** Creates a table showing the current value of each 
 * of the standard CGI variables.
* of the standard CGI variables. 
 * <P>
* <P> 
 * Taken from Core Servlets and JavaServer Pages
* Taken from Core Servlets and JavaServer Pages 
 * from Prentice Hall and Sun Microsystems Press,
* from Prentice Hall and Sun Microsystems Press, 
 * http://www.coreservlets.com/.
* http://www.coreservlets.com/. 
 * © 2000 Marty Hall; may be freely used or adapted.
* © 2000 Marty Hall; may be freely used or adapted. 
 */
*/ 


 public class ShowCGIVariables extends HttpServlet
public class ShowCGIVariables extends HttpServlet  {
{ 
 public void doGet(HttpServletRequest request,
public void doGet(HttpServletRequest request, 
 HttpServletResponse response)
HttpServletResponse response) 

 throws ServletException, IOException
throws ServletException, IOException  {
{ 
 response.setContentType("text/html");
response.setContentType("text/html"); 
 PrintWriter out = response.getWriter();
PrintWriter out = response.getWriter(); 
 String[][] variables =
String[][] variables = 


 {
{  { "AUTH_TYPE", request.getAuthType() },
{ "AUTH_TYPE", request.getAuthType() }, 


 { "CONTENT_LENGTH",
{ "CONTENT_LENGTH", 
 String.valueOf(request.getContentLength()) },
String.valueOf(request.getContentLength()) }, 


 { "CONTENT_TYPE", request.getContentType() },
{ "CONTENT_TYPE", request.getContentType() }, 


 { "DOCUMENT_ROOT",
{ "DOCUMENT_ROOT", 
 getServletContext().getRealPath("/") },
getServletContext().getRealPath("/") }, 


 { "PATH_INFO", request.getPathInfo() },
{ "PATH_INFO", request.getPathInfo() }, 


 { "PATH_TRANSLATED", request.getPathTranslated() },
{ "PATH_TRANSLATED", request.getPathTranslated() }, 


 { "QUERY_STRING", request.getQueryString() },
{ "QUERY_STRING", request.getQueryString() }, 


 { "REMOTE_ADDR", request.getRemoteAddr() },
{ "REMOTE_ADDR", request.getRemoteAddr() }, 


 { "REMOTE_HOST", request.getRemoteHost() },
{ "REMOTE_HOST", request.getRemoteHost() }, 


 { "REMOTE_USER", request.getRemoteUser() },
{ "REMOTE_USER", request.getRemoteUser() }, 


 { "REQUEST_METHOD", request.getMethod() },
{ "REQUEST_METHOD", request.getMethod() }, 


 { "SCRIPT_NAME", request.getServletPath() },
{ "SCRIPT_NAME", request.getServletPath() }, 


 { "SERVER_NAME", request.getServerName() },
{ "SERVER_NAME", request.getServerName() }, 


 { "SERVER_PORT",
{ "SERVER_PORT", 
 String.valueOf(request.getServerPort()) },
String.valueOf(request.getServerPort()) }, 


 { "SERVER_PROTOCOL", request.getProtocol() },
{ "SERVER_PROTOCOL", request.getProtocol() }, 


 { "SERVER_SOFTWARE",
{ "SERVER_SOFTWARE", 
 getServletContext().getServerInfo() }
getServletContext().getServerInfo() } 
 };
}; 
 String title = "Servlet Example: Showing CGI Variables";
String title = "Servlet Example: Showing CGI Variables"; 
 out.println(ServletUtilities.headWithTitle(title) +
out.println(ServletUtilities.headWithTitle(title) + 
 "<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" + 
 "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" + 
 "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
"<TABLE BORDER=1 ALIGN=\"CENTER\">\n" + 
 "<TR BGCOLOR=\"#FFAD00\">\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" + 
 "<TH>CGI Variable Name<TH>Value");
"<TH>CGI Variable Name<TH>Value"); 

 for(int i=0; i<variables.length; i++)
for(int i=0; i<variables.length; i++)  {
{ 
 String varName = variables[i][0];
String varName = variables[i][0]; 
 String varValue = variables[i][1];
String varValue = variables[i][1]; 
 if (varValue == null)
if (varValue == null) 
 varValue = "<I>Not specified</I>";
varValue = "<I>Not specified</I>"; 
 out.println("<TR><TD>" + varName + "<TD>" + varValue);
out.println("<TR><TD>" + varName + "<TD>" + varValue); 
 }
} 
 out.println("</TABLE></BODY></HTML>");
out.println("</TABLE></BODY></HTML>"); 
 }
} 


 /**//** POST and GET requests handled identically. */
/**//** POST and GET requests handled identically. */ 

 public void doPost(HttpServletRequest request,
public void doPost(HttpServletRequest request, 
 HttpServletResponse response)
HttpServletResponse response) 

 throws ServletException, IOException
throws ServletException, IOException  {
{ 
 doGet(request, response);
doGet(request, response); 
 }
} 
 }
} 
