Feedback

Codebox Software

URL Parsing from Java Servlets

Tags:  
Published: 02 Jan 2011

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() httphttp://example.com/showLogin
getServerName() example.comhttp://example.com/showLogin
getServerPort() 80http://example.com/showLogin
getContextPath() empty stringhttp://example.com/showLogin
getServletPath() /showLoginhttp://example.com/showLogin
getPathInfo() nullhttp://example.com/showLogin
getQueryString() nullhttp://example.com/showLogin
getRequestURI() /showLoginhttp://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() httphttp://testserver:8080/MyApp/actions/logout
getServerName() testserverhttp://testserver:8080/MyApp/actions/logout
getServerPort() 8080http://testserver:8080/MyApp/actions/logout
getContextPath() /MyApphttp://testserver:8080/MyApp/actions/logout
getServletPath() /actionshttp://testserver:8080/MyApp/actions/logout
getPathInfo() /logouthttp://testserver:8080/MyApp/actions/logout
getQueryString() nullhttp://testserver:8080/MyApp/actions/logout
getRequestURI() /MyApp/actions/logouthttp://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() httphttp://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens
getServerName() 192.168.1.2http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens
getServerPort() 80http://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens
getContextPath() /TestApphttp://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens
getServletPath() /actions/login.actionhttp://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens
getPathInfo() nullhttp://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens
getQueryString() user=rob&pass=kittenshttp://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens
getRequestURI() /TestApp/actions/login.actionhttp://192.168.1.2/TestApp/actions/login.action?user=rob&pass=kittens

Send Feedback

Use this form to send feedback, if you want a reply please include your email address!