25

From the following URL I need to get (http://localhost:9090/dts) alone.
That is I need to remove (documents/savedoc) (OR)
need to get only - (http://localhost:9090/dts)

http://localhost:9090/dts/documents/savedoc  

Is there any method available in request to get the above?

I tried the following and got the result. But still trying.

System.out.println("URL****************"+request.getRequestURL().toString());  
System.out.println("URI****************"+request.getRequestURI().toString());
System.out.println("ContextPath****************"+request.getContextPath().toString());

URL****************http://localhost:9090/dts/documents/savedoc  
URI****************/dts/documents/savedoc  
ContextPath****************/dts

Can anyone please help me in fixing this?

6 Answers 6

41

You say you want to get exactly:

http://localhost:9090/dts

In your case, the above string consists of:

  1. scheme: http
  2. server host name: localhost
  3. server port: 9090
  4. context path: dts

More info about the elements of a request path can be found in the official Oracle Java EE Tutorial: Getting Information from Requests

First variant:

String scheme = request.getScheme();
String serverName = request.getServerName();
int serverPort = request.getServerPort();
String contextPath = request.getContextPath();  // includes leading forward slash

String resultPath = scheme + "://" + serverName + ":" + serverPort + contextPath;
System.out.println("Result path: " + resultPath);

Second variant:

String scheme = request.getScheme();
String host = request.getHeader("Host");        // includes server name and server port
String contextPath = request.getContextPath();  // includes leading forward slash

String resultPath = scheme + "://" + host + contextPath;
System.out.println("Result path: " + resultPath);

Both variants will give you what you wanted: http://localhost:9090/dts

Of course, there are other variants like others already wrote. It's just in your original question you asked about how to get http://localhost:9090/dts, i.e. you want your path to include the scheme.

But if you don't need the scheme, the quick way is:

String resultPath = request.getHeader("Host") + request.getContextPath();

And you'll get (in your case): localhost:9090/dts

0
20

AFAIK for this there is no API provided method, need to customization.

String serverName = request.getServerName();
int portNumber = request.getServerPort();
String contextPath = request.getContextPath();

// try this

System.out.println(serverName + ":" +portNumber + contextPath );
18

Just remove URI from URL and then append context path to it. No need to fiddle with loose schemes and ports which is only more tedious when you're dealing with default port 80 which don't need to appear in URL at all.

StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String ctx = request.getContextPath();
String base = url.substring(0, url.length() - uri.length() + ctx.length());
// ...

See also:

1
  • 2
    5 similar questions. Finally an answer that is succinct. Commented Apr 23, 2014 at 15:33
3

In my understanding, you need the domain part and Context path only. Based on this understanding, You can use this method to get the required string.

String domain = request.getRequestURL().toString();
String cpath = request.getContextPath().toString();

String tString = domain.subString(0, domain.indexOf(cpath));

tString = tString + cpath;
0
1

For those who want to get, in their endpoint, the URL of the front page which targeted the endpoint. You can use this:

request.getHeader("referer")
0

Usually I have a method like this:

public String getAbsoluteContextPath() throws MalformedURLException {
    ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
    HttpServletRequest request = (HttpServletRequest) context.getRequest();
    URL url = new URL(request.getRequestURL().toString());
    return url.getProtocol() + "://" + url.getAuthority() + context.getRequestContextPath();
}

This method will return what you want, with the port number only if it exists in the current request. In your case it will return: http://localhost:9090/dts

Not the answer you're looking for? Browse other questions tagged or ask your own question.