getRequestURL() vs getRequestURI()

getRequestURL() vs getRequestURI()

getRequestURL() vs getRequestURI() methods are used by Servlet Programmer to retrieve the URL particulars of client.

getRequestURL() and getRequestURI() methods are defined in HttpServletRequest interface. These two are used by the Servlet to retrieve the URL information (written in ACTION attribute of FORM tag) used by the client to call the servlet.

getRequestURL() vs getRequestURI() differ in the length of the URL returned.

Let us see what Java API says about these methods.

  • public abstract java.lang.StringBuffer getRequestURL(): Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
  • public abstract java.lang.String getRequestURI(): Returns the part of this request’s URL from the protocol name up to the query string in the first line of the HTTP request.
Example on getRequestURL() vs getRequestURI(): Let us write a servlet that includes both methods and analyze them.

HTML Program: ClientData.html

web.xml entry for ClientInformation servlet

Servelet Program:

Output screen of ClientData.html with text field filled up.

getRequestURL() vs getRequestURI()

The output screen when submit button is clicked.

getRequestURL() vs getRequestURI()

By looking at the output screen, it is understood that getRequestURL() returns complete URL used by the client where as getRequestURI() returns just the basic folder name (may represent the project name) and the servlet alias name. Anyhow, both will not return the query string (like t1=SNRao).

Pass your comments and suggestions this tutorial getRequestURL() vs getRequestURI().

Leave a Reply

Your email address will not be published. Required fields are marked *