HttpServletRequest 完成 URL [英] HttpServletRequest to complete URL

查看:37
本文介绍了HttpServletRequest 完成 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 HttpServletRequest 对象.

如何获取导致此调用到达我的 servlet 的完整且准确的 URL?

How do I get the complete and exact URL that caused this call to arrive at my servlet?

或者至少尽可能准确,因为也许有些东西可以重新生成(可能是参数的顺序).

Or at least as accurately as possible, as there are perhaps things that can be regenerated (the order of the parameters, perhaps).

推荐答案

HttpServletRequest 有以下方法:

The HttpServletRequest has the following methods:

  • getRequestURL() - returns the part of the full URL before query string separator character ?
  • getQueryString() - returns the part of the full URL after query string separator character ?

因此,要获取完整的 URL,只需执行以下操作:

So, to get the full URL, just do:

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

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

这篇关于HttpServletRequest 完成 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆