Tomcat:getHeader(“Host”)与getServerName()

时间:2012-05-02 03:08:09

标签: java tomcat httprequest

我有一个Tomcat应用程序,它是从多个域提供的。以前的开发人员构建了一个返回应用程序URL的方法(见下文)。在方法中,他们请求服务器名称(request.getServerName()),该名称适当地从 httpd.conf 文件中返回 ServerName

然而,我不希望如此。我想要的是浏览器发送请求的主机名,即浏览器从哪个域访问应用程序。

我尝试了getHeader("Host"),但仍然在 httpd.conf 文件中返回 ServerName

而不是request.getServerName(),我应该使用什么来获取浏览器发送请求的服务器名称?

例如:

  • httpd.conf 中的ServerName: www.myserver.net
  • 用户访问 www.yourserver.net
  • 上的Tomcat应用程序

我需要返回 www.yourserver.net NOT www.myserver.net request.getServerName()来电似乎只会返回 www.myserver.net

/**
 * Convenience method to get the application's URL based on request
 * variables.
 * 
 * @param request the current request
 * @return URL to application
 */
public static String getAppURL(HttpServletRequest request) {
    StringBuffer url = new StringBuffer();
    int port = request.getServerPort();
    if (port < 0) {
        port = 80; // Work around java.net.URL bug
    }
    String scheme = request.getScheme();
    url.append(scheme);
    url.append("://");
    url.append(request.getServerName());
    if (("http".equals(scheme) && (port != 80)) || ("https".equals(scheme) && (port != 443))) {
        url.append(':');
        url.append(port);
    }
    url.append(request.getContextPath());
    return url.toString();
}

提前致谢!

4 个答案:

答案 0 :(得分:16)

您需要确保httpd将客户端提供的Host标头传递给Tomcat。最简单的方法(假设您正在使用mod_proxy_http - 您没有说)具有以下内容:

ProxyPreserveHost On

答案 1 :(得分:5)

如何使用我在此演示JSP中所做的事情?

<%
  String requestURL = request.getRequestURL().toString();
  String servletPath = request.getServletPath();
  String appURL = requestURL.substring(0, requestURL.indexOf(servletPath));
%>
appURL is <%=appURL%>

答案 2 :(得分:1)

可能与此问题无关。 如果您使用的是tomcat,则可以在请求标头中指定任何主机字符串,即使是像<script>alert(document.cookie);</script>这样的javascript

然后它可以显示在页面上。:

<p> host name is : <%= request.getServerName() %> </p>

因此,在使用之前,您需要验证

答案 3 :(得分:-1)

这确实很成问题,因为有时您甚至不知道您希望成为完全限定域的主机在哪里被删除。 @rickz提供了一个很好的解决方案,但这是另一个我认为更完整并涵盖许多不同网址的解决方案:

基本上,您删除协议(http://,https://,ftp://,...)然后删除端口(如果存在),然后删除整个URI。这为您提供了顶级域名和子域名的完整列表。

String requestURL = request.getRequestURL().toString();
String withoutProtocol = requestURL.replaceAll("(.*\\/{2})", "")
String withoutPort = withoutProtocol.replaceAll("(:\\d*)", "") 
String domain = withoutPort.replaceAll("(\\/.*)", "")

我使用内联方法定义在scala中执行此操作,但上面的代码更详细,因为我发现在纯java中发布解决方案更好。因此,如果您为此创建方法,您可以将它们链接起来执行以下操作:

removeURI(removePort(removeProtocol(requestURL)))