Mar*_*all 11 java tomcat httprequest
我有一个Tomcat应用程序,它是从多个域提供的.以前的开发人员构建了一个返回应用程序URL的方法(见下文).在方法中,它们请求服务器名称(request.getServerName()),它适当地从httpd.conf文件返回ServerName.
但是,我不希望这样.我想要的是浏览器发送请求的主机名,即浏览器从哪个域访问应用程序.
我试过getHeader("Host"),但仍然返回httpd.conf文件中设置的ServerName.
而不是request.getServerName(),我应该使用什么来获取浏览器发送请求的服务器名称?
例如:
我需要返回www.yourserver.net而 不是 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();
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
Mar*_*mas 16
您需要确保将客户端提供httpd的Host标头传递给Tomcat.最简单的方法(假设您正在使用mod_proxy_http- 您没有说)具有以下内容:
ProxyPreserveHost On
Run Code Online (Sandbox Code Playgroud)
像我在此演示JSP中所做的那样使用怎么样?
<%
String requestURL = request.getRequestURL().toString();
String servletPath = request.getServletPath();
String appURL = requestURL.substring(0, requestURL.indexOf(servletPath));
%>
appURL is <%=appURL%>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27756 次 |
| 最近记录: |