HttpServletRequest完成URL [英] HttpServletRequest to complete URL

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

问题描述

我有一个 HttpServletRequest 对象。

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

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天全站免登陆