6.1 Overview of CGI Variables If you are learning Java servlet from traditional CGI programming, you may have been accustomed to the concept of "CGI variable". The CGI variable brings together various information about the request: part from the HTTP request command and request head, such as a Content-length header; part from the socket itself, such as the host's name, and IP address; also partially related to the server installation, such as URLs Mapping of the actual path. 6.2 Servlet equivalence of standard CGI variables indicates that the following table assumes that the Request object is a HTTPServletRequest type object that is provided to the Doget and dopost methods. CGI Variable Meaning Access from Doget or Dopost Auth_Type If the Authorization Head is provided, the specific mode is specified here (Basic or Digest). Request.GetAuthType () Content_length is only used for POST requests, indicating the number of bytes sent. Strictly speaking, equivalent expression should be string.valueof (Request.getContentLength ()) (return a string). But more common is to return the same integer with Request.getContentLength (). Content_Type If specified, it means the type of the following data. Request.getContentType () Document_Root with http: // Host / corresponding path. GetServletContext (). GetRealPath ("/") Note The equivalent expression in the low version servlet specification is request.getRealPath ("/"). HTTP_XXX_YYY visits any HTTP header. Request.getHeader ("XXX-YYY") Path_info URL The additional path information in the URL, that is, after the servlet path in the URL, the part before the string is queried. Request.getPathInfo () path_translated path information after mapping the actual path of the server. Request.getPathTranslated () query_string This is a query string that is attached to the URL in the string. The data is still URL encoding. In servlets, there is a lot of unresolved data, which generally use GetParameter to access each parameter. Request.getQueryString () Remote_Addr issues the IP address of the requesting client. Request.getRemoteAddr () Remote_host issues a complete domain name for the request, such as java.sun.com. If the domain name cannot be determined, the IP address is returned. Request.getRemotehost () Remote_User If an Authorization header is provided, it represents its part of the user. It represents the name of the user who issues a request. Request.getRemoteuser () Request_Method request type. Usually GET or POST. But occasionally also appear, and the part of the SBRIPT_NAME URL will also appear in the part. Request.getMethod () Script_name URL, which does not contain additional path information and query strings. Request.getServletPath () Server_name web server name. Request.getServerName () Server_Port server listening to the port.
Strictly speaking, the equivalent expression should be the string.valueof (Request.getServerPort ()) that returns a string. But often use Request.getServerPort () that returns an integer value. Server_Protocol Requests the protocol name and version of the command (ie HTTP / 1.0 or HTTP / 1.1). The name and version of the Request.GetProtocol () Server_Software Servlet engine. GetServletContext (). GetServerInfo () 6.3 Instance: Read the CGI Variable The servlet is created a table that displays all CGI variables other than HTTP_XXX_YYY. HTTP_XXX_YYY is an HTTP request header information, please refer to 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) "
\n " "Title h1> \N " "