1

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?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
somebody
  • 49
  • 11
  • 1
    Check out http://stackoverflow.com/questions/2222238/httpservletrequest-to-complete-url – Pace Jan 22 '13 at 13:23
  • No this doesn't include a ";jsession..." part – somebody Jan 22 '13 at 13:55
  • Probably the jsessionid parameter was not present in the request URL. If cookies are enabled, the jsessionid will be present as an HTTP header. In this case, you can get it with: request.getRequestedSessionId() – Rafael Sisto Jan 22 '13 at 17:04

1 Answers1

2

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();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • +1 for the `;jsessionid=` part but you missed a `)` when appending the query string ;) – Alonso Dominguez Jan 22 '13 at 17:10
  • uff, wrong, this will not work if servlet receives a url containing invalid sessionid. I do not want to be rude but this is not a complete answer, so I am unaccepting it. – somebody Jan 22 '13 at 20:44
  • Ok. What does the `request.getRequestedSessionId()` in such case say? And if it returns the desired value, what does the `request.isRequestedSessionIdFromURL()` and `...FromCookie()` say? – BalusC Jan 22 '13 at 20:46
  • fromCookie returns true, FromURL returns false and I do not know if URL originaly containtained the ;jsessionid – somebody Jan 22 '13 at 21:12