The ServletRequest and HttpServletRequest interfaces have a ton of methods you can call, but you don’t need to memorize them all. On your own, you really should look at the full API for javax.servlet. ServletRequest and javax.servlet.http.HttpServletRequest, but here we’ll look at only the methods you’re most likely to use in your work (and which might also show up on the exam).
In the real world, you’ll be lucky (or unlucky, depending on your perspective), to use more than 15% of the request API. Don’t worry if you aren’t clear about how or why you’d use each of these; we’ll see more details on some of them (especially cookies) later in the book.
The client’s platform and browser info
String client = request.getHeader("User-Agent");
The cookies associated with this request
Cookie[] cookies = request.getCookies();
The session associated with this client
HttpSession session = request.getSession();
The HTTP Method of the request
String theMethod = request.getMethod();
An input stream from the request
ServletInputStream input = getInputStream();
There are no Dumb Questions
Q:
Q: Why would I ever want to get an InputStream from the request?
A:
A: With a GET request, there’s nothing but the request header info. In other words, there’s no body to care about. BUT... with an HTTP POST, there’s body info. Most of the time, all you care about from the body is sucking out the parameter values (for example, “color=dark”) using request.getParameter(), but those values might be large. It is also possible to create a servlet that handles a computer-driven request in which the body of the request holds textual or binary content to be processed. In this case you can use the getReader or getInputStream methods. These streams will only contain the body of the HTTP request and not the headers.
Q:
Q: What’s the difference between getHeader() and getIntHeader()? Far as I can tell, headers are always Strings! Even the getIntHeader() method takes a String representing the name of the header, so what’s the int about?
A:
A: Headers have both a name (like “User-Agent” or “Host”) and a value (like “Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4) Gecko/20030624 Netscape/7.1” or “www.wickedlysmart.com”). The values that come back from headers are always in a String form, but for a few headers, the String represents a number. The “Content-Length” header returns the number of bytes that make up the message-body. The “Max-Forwards” HTTP header, for example, returns an integer indicating how many router hops the request is allowed to make. (You might want to use this header if you’re trying to trace a request that you think is getting stuck in a loop somewhere.)
You could get the value of the “Max-Forwards” header by using getHeader():
String forwards = request.getHeader("Max-Forwards"); int forwardsNum = Integer.parseInt(forwards);
And that works fine. But if you know the value of the header is supposed to represent an int, you can use getIntHeader() as a convenience method to save the extra step of parsing the String to an int:
int forwardsNum = request.getIntHeader("Max-Forwards");
getServerPort(), getLocalPort(), and getRemotePort() are confusing!
The getServerPort() should be obvious... until you ask what getLocalPort() means. So let’s do the easy one first: getRemotePort(). First you should ask, “remote to whom?” In this case, since it’s the server asking, it’s the CLIENT that’s the remote thing. The client is remote to the server, so getRemotePort() means “get the client’s port”. In other words, the port number on the client from which the request was sent. Remember: if you’re a servlet, remote means client.
The difference between getLocalPort() and getServerPort() is more subtle—getServerPort() says, “to which port was the request originally SENT?” while getLocalPort() says, “on which port did the request END UP?” Yes, there’s a difference, because although the requests are sent to a single port (where the server is listening), the server turns around and finds a different local port for each thread so that the app can handle multiple clients at the same time.