somebody
somebody

Reputation: 63

Reading whole url from servlet

I would like to read from a servlet the exact URL that was set in the HTTP request. That is together with any URL rewritten parts (;jsessionid=…).

Is it possible?

Upvotes: 1

Views: 527

Answers (1)

BalusC
BalusC

Reputation: 1108902

You can get the request URL (the part before ; and ?) as follows:

StringBuffer requestURL = request.getRequestURL();

You can check as follows if the session ID was attached as URL path fragment:

if (request.isRequestedSessionIdFromURL()) {
    requestURL.append(";jsessionid=").append(request.getSession().getId());
}

You can get and append the query string as follows, if any:

if (request.getQueryString() != null) {
    requestURL.append('?').append(request.getQueryString());
}

Finally, get the full URL as follows:

String fullURL = requestURL.toString();

Upvotes: 2

Related Questions