How do I get servlet request URL information?

In the example below we extract information about the request object path information. We extract the protocol user, server and and its assigned port number. We extract our application context path, servlet path, path info and the query string information. If we combine all the information below we’ll get something equals to the request.getRequestURL().

package org.kodejava.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletUrlInformation extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);

    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws ServletException, IOException {
        // Getting servlet request URL
        String url = request.getRequestURL().toString();

        // Getting servlet request query string.
        String queryString = request.getQueryString();

        // Getting request information without the hostname.
        String uri = request.getRequestURI();

        // Below we extract information about the request object path
        // information.
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int portNumber = request.getServerPort();
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        String pathInfo = request.getPathInfo();
        String query = request.getQueryString();

        PrintWriter pw = response.getWriter();
        pw.print("Url: " + url + "<br/>");
        pw.print("Uri: " + uri + "<br/>");
        pw.print("Scheme: " + scheme + "<br/>");
        pw.print("Server Name: " + serverName + "<br/>");
        pw.print("Port: " + portNumber + "<br/>");
        pw.print("Context Path: " + contextPath + "<br/>");
        pw.print("Servlet Path: " + servletPath + "<br/>");
        pw.print("Path Info: " + pathInfo + "<br/>");
        pw.print("Query: " + query);

Register the servlet in the web.xml file and define the url-pattern to urlinfo in the servlet-mapping. When you access this servlet using the following url `http://localhost:8080/urlinfo?x=1&y=1`, you’ll get the following output on your browser:

Url: http://localhost:8080/urlinfo
Uri: /urlinfo
Scheme: http
Server Name: localhost
Port: 8080
Context Path: 
Servlet Path: /urlinfo
Path Info: null
Query: x=1&y=1


  1. This works fine only if the requests don’t pass through a load balancer. In that case, server name and server port will refer to the load balancer and not to the localhost.

  2. Hi Stefano,

    For this case you might want to check the value of the following http request header:

    • X-Forwarded-For
    • Proxy-Client-IP
    • WL-Proxy-Client-IP
  3. I have a page that have local layout taglibs. When I process request which has taglib url I expect to retrieve master url, I see that it’s stored in request.requestDispatcherPath, but I haven’t found a way how to retrieve that. Maybe you could help with that?


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.