天天看点

Java Servlet环境中获取完整的客户端请求地址

客户端请求服务的地址中除了基本的协议,主机,端口信息外,还可能包含查询串,若想获取完整的请求地址,则需要考虑到存在查询串的可能性。

一个获取完整客户端请求地址的方法:

public static String getFullURL(HttpServletRequest request) {
    StringBuffer requestURL = request.getRequestURL();
    String queryString = request.getQueryString();

    if (queryString == null) {
        return requestURL.toString();
    } else {
        return requestURL.append('?').append(queryString).toString();
    }}