Question

So here are 2 requests:

  1. http://example.com/someUrl/
  2. http://example.com/someUrl/index.xhtml (xhtml extension is not relevant just an example)

When the <welcome-file>index.xhtml</welcome-file> is been set, request 1 is handled by the server as 2.

However, in both cases the request.getRequestURI() returns the complete URI: someUrl/index.xhtml.

According to documentation it shouldn't but in most cases it's what we want so it seems fine it does.

I'm working with JSF under JBoss Wildfly (Undertow webservice) and I don't know which one is responsible.

I don't necessarily want to change how it works but I'm looking for a way of getting the original URI as the enduser sees in browser address bar, thus without the index.xhtml part in case of 1.

To be more precise, I have to get the exact same URL as returned by document.location.href in JavaScript.

Was it helpful?

Solution

The welcome file is been displayed by a forward which is under the server's covers been performed by RequestDispatcher#forward(). In that case, the original request URI is available as a request attribute with a key as identified by RequestDispatcher#FORWARD_REQUEST_URI, which is javax.servlet.forward.request_uri.

So, this should do:

String originalURI = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);

if (originalURI == null) {
    originalURI = request.getRequestURI();
}

// ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top