Codebox Software
URL Parsing from Java Servlets
Published:
This page is intended to provide a simple guide for Java Servlet developers, who need to extract parts of request URLs
using the methods of the HttpServletRequest
class.
The methods provided by the API can be a little confusing to use - the names don't always make it clear what they are supposed return, and when
there is no value available some of them return null while others pass back an empty string. The values returned
also change depending on how the url is mapped to the servlet via the <url-pattern>
elements
in the web.xml deployment descriptor.
The following examples cover most of the common use-cases. Note that the value returned by getRequestURI()
depends on exactly how the resource was specified in the HTTP request, the most likely value is shown in the examples but
you may get something different under certain circumstances.
Example 1
URL: | http://example.com/showLogin |
web.xml | <url-pattern>/showLogin</url-pattern> |
Here the web application is deployed to the root context (so it's probably the only application running on this server), and is accessed using http on port 80.
getScheme() | http | http://example.com/showLogin |
getServerName() | example.com | http://example.com/showLogin |
getServerPort() | 80 | http://example.com/showLogin |
getContextPath() | empty string | http://example.com/showLogin |
getServletPath() | /showLogin | http://example.com/showLogin |
getPathInfo() | null | http://example.com/showLogin |
getQueryString() | null | http://example.com/showLogin |
getRequestURI() | /showLogin | http://example.com/showLogin |
Example 2
URL: | http://testserver:8080/MyApp/actions/logout |
web.xml | <url-pattern>/actions/*</url-pattern> |
This example is more complicated, the application is running in the context 'MyApp' (this was the name given to the application when it was deployed, it may be running along side other applications on the server), and the server uses a non-standard port (8080). The web.xml deployment descriptor maps any requests that have '/actions/' after the context name to this servlet:
getScheme() | http | http://testserver:8080/MyApp/actions/logout |
getServerName() | testserver | http://testserver:8080/MyApp/actions/logout |
getServerPort() | 8080 | http://testserver:8080/MyApp/actions/logout |
getContextPath() | /MyApp | http://testserver:8080/MyApp/actions/logout |
getServletPath() | /actions | http://testserver:8080/MyApp/actions/logout |
getPathInfo() | /logout | http://testserver:8080/MyApp/actions/logout |
getQueryString() | null | http://testserver:8080/MyApp/actions/logout |
getRequestURI() | /MyApp/actions/logout | http://testserver:8080/MyApp/actions/logout |
Example 3
URL: | http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens |
web.xml | <url-pattern>*.action</url-pattern> |
In this example, the application is running in the context 'TestApp', and the request includes some parameters. The web.xml deployment descriptor maps any requests that end '.action' to this servlet:
getScheme() | http | http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens |
getServerName() | 192.168.1.2 | http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens |
getServerPort() | 80 | http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens |
getContextPath() | /TestApp | http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens |
getServletPath() | /actions/login.action | http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens |
getPathInfo() | null | http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens |
getQueryString() | user=rob&pass=kittens | http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens |
getRequestURI() | /TestApp/actions/login.action | http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens |