4. Retrieving Information

Java Servlet Programming, 2nd Edition > 4. Retrieving Information

 
< BACKCONTINUE >

Chapter 4. Retrieving Information

To build a 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 the 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 information about the requests that the application is handling.

A number of methods provide servlets access to this information. For the most part, each method returns one specific result. Compared this to the way environment variables are used to pass a CGI program its information, the servlet approach has several advantages:

  • Stronger type checking. Servlets get more help from the compiler in catching errors. A CGI program uses one function to retrieve its environment variables. Many errors cannot be found until they cause runtime problems. Let's look at how a CGI program and a servlet find the port on which its server is running.

    A CGI script written in Perl calls:

    $port = $ENV{'SERVER_PORT'};

    where $port is an untyped variable. A CGI program written in C calls:

    char *port = getenv("SERVER_PORT");

    where port is a pointer to a character string. The chance for accidental errors is high. The environment variable name could be misspelled (it happens often enough) or the datatype might not match what the environment variable returns.

    A servlet, on the other hand, calls:

    int port = req.getServerPort()

    This eliminates a lot of accidental errors because the compiler can guarantee there are no misspellings and each return type is as it should be.

  • Delayed calculation. When a 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 a servlet has the option to improve performance by delaying these calculations and performing them on demand as needed.

  • More interaction with the server. Once a CGI program begins execution, it is untethered from its server. The only communication path available to the program is its standard output. A servlet, however, can work with the server. As discussed in the previous chapter, a servlet operates either within the server (when possible) or as a connected process outside the server (when necessary). Using this connectivity, a servlet can make ad hoc requests for calculated information that only the server can provide. For example, a servlet can have its server do arbitrary path translations, taking into consideration the server's aliases and virtual paths.

If you're coming to servlets from CGI, Table 4-1 is a "cheat sheet" you can use for your migration. It lists each CGI environment variable and the corresponding HTTP servlet method.

Table 4-1, 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

getServletContext().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")

In the rest of this chapter, we'll see how and when to use these methods and many other methods that have no CGI counterparts. Along the way, we'll put the methods to use in some real servlets.


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net