Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpServletRequest to complete URL

I have an HttpServletRequest object.

How do I get the complete and exact URL that caused this call to arrive at my servlet?

Or at least as accurately as possible, as there are perhaps things that can be regenerated (the order of the parameters, perhaps).

like image 984
flybywire Avatar asked Feb 08 '10 14:02

flybywire


People also ask

What is the difference between HttpServletRequest and ServletRequest?

The ServletRequest and HttpServletRequest classes hold all of the accessible information about the client and the server. HttpServletRequest is a subclass of ServletRequest, and is passed to each servlet handler method (such as doGet(..), doPut(..), etc.)

How do I find my Java server URL?

In Java, you can use InetAddress. getLocalHost() to get the Ip Address of the current Server running the Java app and InetAddress. getHostName() to get Hostname of the current Server name.

What does getRequestURI return?

The function getRequestURI() returns the complete requested URI. This includes the deployment folder and servlet-mapping string. It will also return all extra path information.

What is ServletRequest?

A ServletRequest object provides data including parameter name and values, attributes, and an input stream. Interfaces that extend ServletRequest can provide additional protocol-specific data (for example, HTTP data is provided by HttpServletRequest .


2 Answers

The HttpServletRequest has the following methods:

  • getRequestURL() - returns the part of the full URL before query string separator character ?
  • getQueryString() - returns the part of the full URL after query string separator character ?

So, to get the full URL, just do:

public static String getFullURL(HttpServletRequest request) {     StringBuilder requestURL = new StringBuilder(request.getRequestURL().toString());     String queryString = request.getQueryString();      if (queryString == null) {         return requestURL.toString();     } else {         return requestURL.append('?').append(queryString).toString();     } } 
like image 73
Bozho Avatar answered Sep 28 '22 22:09

Bozho


I use this method:

public static String getURL(HttpServletRequest req) {      String scheme = req.getScheme();             // http     String serverName = req.getServerName();     // hostname.com     int serverPort = req.getServerPort();        // 80     String contextPath = req.getContextPath();   // /mywebapp     String servletPath = req.getServletPath();   // /servlet/MyServlet     String pathInfo = req.getPathInfo();         // /a/b;c=123     String queryString = req.getQueryString();          // d=789      // Reconstruct original requesting URL     StringBuilder url = new StringBuilder();     url.append(scheme).append("://").append(serverName);      if (serverPort != 80 && serverPort != 443) {         url.append(":").append(serverPort);     }      url.append(contextPath).append(servletPath);      if (pathInfo != null) {         url.append(pathInfo);     }     if (queryString != null) {         url.append("?").append(queryString);     }     return url.toString(); } 
like image 26
MatBanik Avatar answered Sep 28 '22 23:09

MatBanik