JSP或JavaScript相当于PHP';s$服务器[“HTTP”主机]?
我的JavaScript中有一个绝对URL,我已经为window.location硬编码了它 我不想每次测试我的应用程序时都要改变这一点。在PHP中,我会通过测试$_SERVER[“HTTP_HOST”]变量来确定我在哪台服务器上,并进行相应的调整来处理这个问题。但是,我对Java不太熟悉,我想知道它是否有类似的方法?或者JavaScript是否也有类似的方法 代码如下:JSP或JavaScript相当于PHP';s$服务器[“HTTP”主机]?,java,php,javascript,url,window.location,Java,Php,Javascript,Url,Window.location,我的JavaScript中有一个绝对URL,我已经为window.location硬编码了它 我不想每次测试我的应用程序时都要改变这一点。在PHP中,我会通过测试$_SERVER[“HTTP_HOST”]变量来确定我在哪台服务器上,并进行相应的调整来处理这个问题。但是,我对Java不太熟悉,我想知道它是否有类似的方法?或者JavaScript是否也有类似的方法 代码如下: var url = "http://172.17.1.107/store/results/index.jsp"; windo
var url = "http://172.17.1.107/store/results/index.jsp";
window.location = url;
我想做的是:
var server = [something that returns just 172.17.1.107 (with or without the http:// is fine)]
var url = "http://" + server + "/store/results/index.jsp";
window.location = url;
在PHP中,我会这样做:
var server = <?= $_SERVER["HTTP_HOST"] ?>
var url = "http://" + server + "/store/results/index.php";
window.location = url;
var服务器=
var url=“http://“+server+”/store/results/index.php”;
window.location=url;
有什么想法吗?我假设我是在这样的假设下操作的,即您必须使用绝对URL来更改JavaScript中当前窗口的位置。如果有其他方法可以在JavaScript中更改窗口位置而不使用绝对URL,请随时提供
提前感谢…您确实应该搜索这个,但在JSP中它是:
request.getRemoteHost()
Javascript:
var server = window.location.hostname;
位置对象,您想要的是
或者,您可以选择只使用根相对URL,只设置属性,而不干扰主机业务
location.pathname = "/store/results/index.jsp";
您需要的是:
request.getServerName()
例如:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>