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 combaine all the information below we’ll get someting equals to the request.getRequestURL().

package org.kodejava.example.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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();

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        pw.print("Url: " + url + "");
        pw.print("Uri: " + uri + "");
        pw.print("Scheme: " + scheme + "");
        pw.print("Server Name: " + serverName + "");
        pw.print("Port: " + portNumber + "");
        pw.print("Context Path: " + contextPath + "");
        pw.print("Servlet Path: " + servletPath + "");
        pw.print("Path Info: " + pathInfo + "");
        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

Wayan Saryada

I am a programmer, a runner, an open water diver and currently living in the island of Bali, Indonesia. If you need help on Java programming you can hire me on Fiverr.

Leave a Comment.