Cgi|js|servlet| Variable | access
6.1 CGI Variable overview
If you're learning the Java Servlet from a traditional CGI program, you might have gotten used to the notion of "CGI variables." The CGI variable brings together a variety of information about the request:
Partly from HTTP request commands and request headers, such as content-length headers;
Part from the socket itself, such as the host's name and IP address;
There are also parts related to the server installation configuration, such as the mapping of URLs to actual paths.
servlet equivalence representation of 6.2 standard CGI variables
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 a authorization header is provided, a specific pattern (basic or Digest) is specified here. Request.getauthtype ()
Content_length is used only for post requests, representing the number of bytes of data sent. Strictly speaking, the equivalent expression should be string.valueof (Request.getcontentlength ()) (return a string). But it is more common to return an integer with the same meaning with request.getcontentlength ().
Content_Type, if specified, represents the type of data followed. Request.getcontenttype ()
Document_root the path corresponding to the http://host/. Getservletcontext (). Getrealpath ("/")
Note that the equivalent expression in the low version servlet specification is Request.getrealpath ("/").
HTTP_XXX_YYY access to any HTTP headers. Request.getheader ("xxx-yyy")
Path_info the additional path information in the URL, that is, after the servlet path in the URL, before the query string. Request.getpathinfo ()
Path_translated The path information after the actual path to the server is mapped. Request.getpathtranslated ()
Query_string This is the string form of the query string appended to the URL, and the data is still URL-coded. In the servlet, there is little need to use data that is not decoded, typically using getparameter to access individual parameters. Request.getquerystring ()
REMOTE_ADDR the IP address of the client that issued the request. REQUEST.GETREMOTEADDR ()
Remote_host the full domain name of the client that made the request, such as java.sun.com. If the domain name cannot be determined, the IP address is returned. Request.getremotehost ()
Remote_user If a authorization header is provided, it represents its user portion. It represents the name of the user who made the request. Request.getremoteuser ()
Request_method request type. Usually a get or post. But occasionally there will be head,put, delete,options, or TRACE. Request.getmethod ()
The part of the servlet that is invoked in the Script_name URL does not contain additional path information and a query string. Request.getservletpath ()
server_name the name of the Web server. Request.getservername ()
Server_port the port on which the server listens. Strictly speaking, the equivalent expression should be the string.valueof (Request.getserverport ()) that returns the string. However, Request.getserverport (), which returns an integer value, is often used.
Server_protocol the Protocol name and version (i.e. http/1.0 or http/1.1) in the request command. Request.getprotocol ()
Server_software the name and version of the servlet engine. Getservletcontext (). Getserverinfo ()
6.3 instance: Reading CGI variables
The following servlet creates a table that shows all the CGI variables except HTTP_XXX_YYY. HTTP_XXX_YYY is the HTTP request header information, as described in 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 = "Show CGI variables";
Out.println (title) + Servletutilities.headwithtitle
"<body bgcolor=\" #FDF5E6 \ ">\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);
}
}