Get full URL and query string in Servlet for both HTTP and HTTPS requests
Simply Use:
String Uri = request.getRequestURL()+"?"+request.getQueryString();
By design, getRequestURL()
gives you the full URL, missing only the query string.
In HttpServletRequest
, you can get individual parts of the URI using the methods below:
// Example: http://myhost:8080/people?lastname=Fox&age=30
String uri = request.getScheme() + "://" + // "http" + "://
request.getServerName() + // "myhost"
":" + // ":"
request.getServerPort() + // "8080"
request.getRequestURI() + // "/people"
"?" + // "?"
request.getQueryString(); // "lastname=Fox&age=30"
.getScheme()
will give you"https"
if it was ahttps://domain
request..getServerName()
givesdomain
onhttp(s)://domain
..getServerPort()
will give you the port.
Use the snippet below:
String uri = request.getScheme() + "://" +
request.getServerName() +
("http".equals(request.getScheme()) && request.getServerPort() == 80 || "https".equals(request.getScheme()) && request.getServerPort() == 443 ? "" : ":" + request.getServerPort() ) +
request.getRequestURI() +
(request.getQueryString() != null ? "?" + request.getQueryString() : "");
This snippet above will get the full URI, hiding the port if the default one was used, and not adding the "?"
and the query string if the latter was not provided.
Proxied requests
Note, that if your request passes through a proxy, you need to look at the X-Forwarded-Proto
header since the scheme might be altered:
request.getHeader("X-Forwarded-Proto")
Also, a common header is X-Forwarded-For
, which show the original request IP instead of the proxys IP.
request.getHeader("X-Forwarded-For")
If you are responsible for the configuration of the proxy/load balancer yourself, you need to ensure that these headers are set upon forwarding.
The fact that a HTTPS
request becomes HTTP
when you tried to construct the URL on server side indicates that you might have a proxy/load balancer (nginx
, pound
, etc.) offloading SSL encryption in front and forward to your back end service in plain HTTP
.
If that's case, check,
- whether your proxy has been set up to forward headers correctly (
Host
,X-forwarded-proto
,X-forwarded-for
, etc). - whether your service container (E.g.
Tomcat
) is set up to recognize the proxy in front. For example,Tomcat
requires addingsecure="true" scheme="https" proxyPort="443"
attributes to itsConnector
- whether your code, or service container is processing the headers correctly. For example,
Tomcat
automatically replacesscheme
,remoteAddr
, etc. values when you addRemoteIpValve
to itsEngine
. (see Configuration guide, JavaDoc) so you don't have to process these headers in your code manually.
Incorrect proxy header values could result in incorrect output when request.getRequestURI()
or request.getRequestURL()
attempts to construct the originating URL.
I know this is a Java question, but if you're using Kotlin you can do this quite nicely:
val uri = request.run {
if (queryString.isNullOrBlank()) requestURI else "$requestURI?$queryString"
}