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

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: