Documenting HttpServletRequest
Posted: December 20th, 2009 | Author: David | Filed under: Programming | Tags: api, httpservletrequest, java, reference | Comments OffFor such a core object the HttpServletRequest javadoc is a little lacking in the Examples Dept. when it comes to documented output. With various methods returning various parts of URLs, it’s often easy to pick the wrong one, so I thought I’d knock up a little table with the getters which trip me up sometimes.
This assumes a simple webapp, living on a server on the local machine, sitting under the context called ‘context’, responding to the URL:
http://localhost:8080/context/hello/world?foo=bar
| Method | Response | Notes |
|---|---|---|
| getContextPath() | /context | The context part of the URL. Should be obvious. |
| getPathInfo() | /hello/world | The part of the URL after the context, but not including the query string. |
| getPathTranslated() | /Users/david/projects/java/hello-world-webapp/target/context/hello/world | Hrm. Sort of where this would be on the local filesystem, but not really. |
| getProtocol() | HTTP/1.1 | This doesn’t produce anything verbatum from the URL, such as ‘http’ or ‘https’ or ‘ftp’ or… |
| getQueryString() | foo=bar | Like it says, the query string. |
| getRequestURI() | /context/hello/world | Everything from but not including the port, up to but not including the query string. |
| getRequestURL() | http://localhost:8080/context/hello/world | This is a StringBuffer object, containing everything but the query string. |
| getServerName() | localhost | The server name as presented in the URL, not the hostname of the box. |
| getServerPort() | 8080 | As expected. Good, good. |
| getServletPath() | This is the path that the servlet is configured (e.g. in web.xml) to respond to relative to the context. It’ll be an empty-string if, as here, this is in response to a wildcard mapping “/*”. |