6.1 CGI variable Overview
If you are switching from traditional CGI programming to learning Java Servlet, you may have become accustomed to the concept of "CGI variable. CGI variables collect various request information:
Some are from HTTP request commands and request headers, such as the Content-Length header;
Some are from the Socket itself, such as the host name and IP address;
Some are also related to the Server Installation configuration, for example, the ing from the URL to the actual path.
6.2 Servlet equivalent representation of Standard CGI variables
The following table assumes that the request object is an HttpServletRequest type object provided to the doGet and doPost methods. CGI variable meaning access from doGet or doPost
If the Authorization header is provided for AUTH_TYPE, the specific mode (basic or digest) is specified here ). Request. getAuthType ()
CONTENT_LENGTH is only used for POST requests, indicating the number of bytes of data sent. Strictly speaking, the equivalent expression should be String. valueOf (request. getContentLength () (returns a String ). However, more commonly, request. getContentLength () is used to return integers with the same meaning.
If CONTENT_TYPE is specified, it indicates the type of the subsequent data. Request. getContentType ()
The path of DOCUMENT_ROOT and http: // host. GetServletContext (). getRealPath ("/")
Note that the equivalent expression in the Servlet specification of lower versions is request. getRealPath ("/").
HTTP_XXX_YYY to access any HTTP header. Request. getHeader ("Xxx-Yyy ")
The additional path information in the PATH_INFO URL, that is, the part after the Servlet Path in the URL and before the query string. Request. getPathInfo ()
PATH_TRANSLATED maps the path information after the actual path of the server. Request. getPathTranslated ()
QUERY_STRING this is the query string appended to the URL in the form of a string. The data is still URL encoded. In Servlet, undecoded data is rarely used. Generally, getParameter is used to access various parameters. Request. getQueryString ()
The IP address of the client that REMOTE_ADDR sends the request. Request. getRemoteAddr ()
The complete domain name of the client that REMOTE_HOST sends the request, such as java.sun.com. If the domain name cannot be determined, the IP address is returned. Request. getRemoteHost ()
REMOTE_USER if the Authorization header is provided, it indicates the user part. It indicates the name of the user who sends the request. Request. getRemoteUser ()
REQUEST_METHOD request type. Usually GET or POST. However, sometimes HEAD, PUT, DELETE, OPTIONS, or TRACE. request. getMethod () appears ()
The part of the Servlet called in the SCRIPT_NAME URL does not contain additional path information and query strings. Request. getServletPath ()
SERVER_NAME Web server name. Request. getServerName ()
The port that the server listens. Strictly speaking, the equivalent expression should be the String. valueOf (request. getServerPort () of the returned String ()). However, the request. getServerPort () that returns an integer is often used ().
The protocol name and version (HTTP/1.0 or HTTP/1.1) in the SERVER_PROTOCOL Request command ). Request. getProtocol ()
SERVER_SOFTWARE Servlet Engine name and version. GetServletContext (). getServerInfo ()
6.3 instance: Read CGI variables
The Servlet below creates a table showing all CGI variables except HTTP_XXX_YYY. HTTP_XXX_YYY is the HTTP request header. For more information, see the previous section.
ShowCGIVariables. java
Package hall;
Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. util .*;
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 = "display 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 [I] [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> ");
}
Public void doPost (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}
}