| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
| Accessing the Standard CGI Variables |
|
To build the successful web application, you often need to know a lot about the environment in which it is running.
You may need to find out about server that is executing your servlets or the specifics of the client that is sending requests.
And no matter what kind of environment the application is running in, you most certainly need the information about the requests that the application is handling.
|
|
Advantages of Using Servlets Over CGI
|
|
- Stronger type checking. In other words, more help from compiler in
catching errors. A CGI program uses one function to retrieve its environment
variable. Many errors cannot be found until they cause runtime problem.
Let's look at how both a CGI program and servlets find the port on which
its server is running.
A CGI script written in Perl call:
$port = $ENV{'SERVER_PORT'};
port is an untyped variable. A CGI
program written in C call:
The chance for accidental errors is also high. The environment variable name
could be misspelled or the data type might not
match what the environment variable returns.
A servlet, on the other hand, call:
int port = req.getServerPort()
This eliminates a lot of the accidental errors because the compiler can
guarantee there are no misspellings and each return type is as it should be.
Delayed calculation. When the server launches a CGI program, the value for
each and every environment variable must be precalculated and passed,
whether the CGI program uses it or not. A server launching the servlets has the
option to improve performance by delaying these calculations and performing
them on demand as needed.
More interaction with server. Once the CGI program begins execution,
it is untethered from its server. The only communication path available to
program is its standard output. A servlet, however, can also work with the
server. As discussed in the last chapter, a servlet operates either within
the server (when possible) or as connected process outside the server
(when necessary). Using this connectivity, a servlet can make ad hoc
requests for calculated the information that only the server can provide. For
example, a servlet can have its server do arbitrary path translation,
taking into consideration the server's aliases and virtual paths.
|
|
CGI Environment Variables and the Corresponding Servlet Methods
|
| CGI Environment Variable |
HTTP Servlet Method |
| SERVER_NAME |
req.getServerName() |
| SERVER_SOFTWARE |
getServletContext().getServerInfo() |
| SERVER_PROTOCOL |
req.getProtocol() |
| SERVER_PORT |
req.getServerPort() |
| REQUEST_METHOD |
req.getMethod() |
| PATH_INFO |
req.getPathInfo() |
| PATH_TRANSLATED |
req.getPathTranslated() |
| SCRIPT_NAME |
req.getServletPath() |
| DOCUMENT_ROOT |
req.getRealPath("/") |
| QUERY_STRING |
req.getQueryString() |
| REMOTE_HOST |
req.getRemoteHost() |
| REMOTE_ADDR |
req.getRemoteAddr() |
| AUTH_TYPE |
req.getAuthType() |
| REMOTE_USER |
req.getRemoteUser() |
| CONTENT_TYPE |
req.getContentType() |
| CONTENT_LENGTH |
req.getContentLength() |
| HTTP_ACCEPT |
req.getHeader("Accept") |
| HTTP_USER_AGENT |
req.getHeader("User-Agent") |
| HTTP_REFERER |
req.getHeader("Referer") |
|
|
The following example reading all the CGI variables
|
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
* 1999 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 boder=1 align="center"\n" +
"<tr bgcolor=\"#FFAD00\">\n" +
"<th>CGI Variable Name<h1>Value");
for(int i=0; i
{
String varName = variables[i][0];
String varValue = variables[i][1];
if (varValue == null)
varValue = "Not specified";
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);
}
}
|
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
cgi tutorial,servlets tutorial,php variables,perl variables,cgi scripts,java servlets,mysql variables,cgi forms
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|