Package coreservlets;
Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. util .*;
/** Creates a table showing the current value of each
* Of the standard CGI variables.
* <P>
* Taken from Core Servlets and assumerver Pages
* From Prentice Hall and Sun Microsystems Press,
* Http://www.coreservlets.com /.
*©2000 Marty Hall; may be freely used or adapted.
*/
Public class ShowCGIVariables extends HttpServlet {
Public void doGet (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
PrintWriter out = response. getWriter ();
String [] [] variables =
{"AUTH_TYPE", request. getAuthType ()},
{"CONTENT_LENGTH ",
String. valueOf (request. getContentLength ())},
{"CONTENT_TYPE", request. getContentType ()},
{"DOCUMENT_ROOT ",
GetServletContext (). getRealPath ("/")},
{"PATH_INFO", request. getPathInfo ()},
{"PATH_TRANSLATED", request. getPathTranslated ()},
{"QUERY_STRING", request. getQueryString ()},
{"REMOTE_ADDR", request. getRemoteAddr ()},
{"REMOTE_HOST", request. getRemoteHost ()},
{"REMOTE_USER", request. getRemoteUser ()},
{"REQUEST_METHOD", request. getMethod ()},
{"SCRIPT_NAME", request. getServletPath ()},
{"SERVER_NAME", request. getServerName ()},
{"SERVER_PORT ",
String. valueOf (request. getServerPort ())},
{"SERVER_PROTOCOL", request. getProtocol ()},
{"SERVER_SOFTWARE ",
GetServletContext (). getServerInfo ()}
};
String title = "Servlet Example: Showing CGI Variables ";
Out. println (ServletUtilities. headWithTitle (title) +
"<Body bgcolor = \" # FDF5E6 \ "> \ n" +
"<H1 ALIGN = \" CENTER \ ">" + title + "</H1> \ n" +
"<Table border = 1 ALIGN = \" CENTER \ "> \ n" +
"<Tr bgcolor = \" # FFAD00 \ "> \ n" +
"<TH> CGI Variable Name <TH> Value ");
For (int I = 0; I <variables. length; I ++ ){
String varName = variables [0];
String varValue = variables [I] [1];
If (varValue = null)
VarValue = "<I> Not specified </I> ";
Out. println ("<TR> <TD>" + varName + "<TD>" + varValue );
}
Out. println ("</TABLE> </BODY> </HTML> ");
}
/** POST and GET requests handled identically .*/
Public void doPost (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}
}