Jeff
Jeff

Reputation: 14279

How do I get a rewritten URL in JSP?

I have a JSP page at an address like this:

http://example.com/foo/bar/rawr/something.jsp

When I output request.getRequestURL(), I get something totally different:

http://111.111.111.111/rawr/something.jsp

Note the domain changed to an IP and /foo/bar is missing. How do I get the true URL that the browser requested using JSP?

Upvotes: 0

Views: 1016

Answers (2)

Moob
Moob

Reputation: 16184

You can use the following to get a rewritten URL excluding domain:

String rewrittenURL = request.getHeader("REDIRECT_URL");

Upvotes: 0

BalusC
BalusC

Reputation: 1108537

This can happen if there is a proxy (such as Apache HTTPD) in front of the Java EE server. The particular proxy could (should) have set the original request URL as a request header. At least, all self-respected proxies do that.

You can crawl through all request headers as follows to find it out:

for (String name : Collections.list(request.getHeaderNames())) {
    System.out.println(name + "=" + Collections.list(request.getHeaders(name)));
}

Upvotes: 1

Related Questions