1. Introduction to CGI VariablesIf you come to Java servlets from traditional CGI, you are probably used to the idea of "CGI Variables". These are a somewhat eclectic collection of information about the request. Some are derived from the HTTP request line and headers (eg the part of the uri That Came After the Question Mark)
Content-Length header), some are derived from the socket itself (e.g. the name and IP address of the requesting host), and some are derived from server installation parameters (e.g. the mapping of URLs to actual paths).
2. Servlet Equivalent of Standard CGI VariablesAlthough it probably makes more sense to think of different sources of data (request data, server data, etc.) as distinct, experienced CGI programmers may find the following table useful. Assume that
Request is the
HttpservletRequest Supplied to the
Doget and
Dopost Methods.
CGI Variable Meaning Accessing it from doGet or doPost AUTH_TYPE If an Authorization header was supplied, this is the scheme specified (basic or digest) request.getAuthType () CONTENT_LENGTH For POST requests only, the number of bytes of data sent. Technically, the equivalent is String.valueOf (request.getContentLength ()) (a String) but you'll probably want to just call request.getContentLength (), which returns an int. CONTENT_TYPE MIME type of attached data, if specified. request.getContentType () DOCUMENT_ROOT Path to directory corresponding to http:. // host / getServletContext () getRealPath ( "/") Note that this was request.getRealPath ( "/") in older servlet specifications HTTP_XXX_YYY Access to arbitrary HTTP headers request.getHeader ( ". xxx-Yyy ") PATH_INFO Path information attached to the URL. Since servlets, unlike standard CGI programs, can talk to the server, they do not need to treat this separately. Path info could be sent as part of the regular form data. Request.getPathInfo () Path_TRA NSLATED The path information mapped to a real path on the server. Again, with servlets there is no need to have a special case for this. Request.getPathTranslated () QUERY_STRING For GET requests, this is the attached data as one big string, with . values still URL-encoded You rarely want the raw data in servlets;. instead use request.getParameter to access individual parameters request.getQueryString () REMOTE_ADDR The IP address of the client that made the request, as a String Eg "192.9.. 48.9 ". Request.getRemoteaddddr () Remote_host the flly qualified domain name (eg" java.sun.com "
) Of the client that made the request. The IP address is returned if this can not be determined. Request.getRemoteHost () REMOTE_USER If an Authorization header was supplied, the user part. Request.getRemoteUser () REQUEST_METHOD The request type, which is usually GET or POST, but is occasionally HEAD, PUT, DELETE, OPTIONS, or TRACE. request.getMethod () SCRIPT_NAME Path to servlet. request.getServletPath () SERVER_NAME Web server's name request.getServerName () SERVER_PORT Port server is listening on. Technically , the equivalent is String.valueOf (request.getServerPort ()), which returns a String. You'll usually just want request.getServerPort (), which returns an int. SERVER_PROTOCOL Name and version used in the request line (eg HTTP / ... 1.0 or HTTP / 1.1) request.getProtocol () SERVER_SOFTWARE Identifying information about the Web server getServletContext () getServerInfo () 3 Example: Reading the CGI VariablesHere's a servlet that creates a table showing the values of all the CGI variables other Than
HTTP_XXX_YYY, WHICH Are Just The Http Request Headers Shown in The Previous Section.
3.1 showcgivariables.javayou can Also
Download the Source OR
Try it on-line. Note: Also Uses
ServletUtilities.java, Shown Earlier.
Package Hall;
Import java.io. *;
Import javax.servlet. *;
Import javax.servlet.http. *;
Import java.util. *;
/ ** Creates a Table Showing The Values of All The CGI Variables.
*
* Part of Tutorial ON Servlets and JSP That Appears At
* http://www.apl.jhu.edu/~hall/java/servlet-tutorial/
* 1999 Marty Hall; May Be freely buy 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)
"
/ n""
"