Getting request URL in a servlet

Posted in :

透過下列的範例,取得目前使用者執行中的網址。

The getRequestURL() omits the port when it is 80 while the scheme is http, or when it is 443 while the scheme is https.

So, just use getRequestURL() if all you want is obtaining the entire URL. This does however not include the GET query string. You may want to construct it as follows then:

StringBuffer requestURL = request.getRequestURL();
if (request.getQueryString() != null) {
    requestURL.append("?").append(request.getQueryString());
}
String completeURL = requestURL.toString();

資料來源:
https://stackoverflow.com/questions/4040094/getting-request-url-in-a-servlet

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *