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?
AFAIK for this there is no API provided method, need to customization.
// try this
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.
You say you want to get exactly:
In your case, the above string consist 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:
Second variant:
Both variants will give you what you wanted:
http://localhost:9090/dts
Of course there are others 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 scheme.In case you still doesn't need a scheme, the quick way is:
And you'll get (in your case):
localhost:9090/dts
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.See also: