JSP或JavaScript等效于PHP的$ _SERVER [" HTTP_HOST"]? [英] JSP or JavaScript equivalent to PHP's $_SERVER["HTTP_HOST"]?

查看:110
本文介绍了JSP或JavaScript等效于PHP的$ _SERVER [" HTTP_HOST"]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的JavaScript中使用了一个绝对URL,我已经为window.location编写了硬编码。

I've go an absolute URL in my JavaScript that I have hard coded for window.location.

我不想每次都改变这个我正在测试我的应用。在PHP中,我会通过测试$ _SERVER [HTTP_HOST]变量来找出我所在的服务器并进行相应调整。但是,我对Java并不熟悉,并且想知道它是否有类似的方法?或者,即使JavaScript有类似的方法?

I don't want to have to change this every time I am testing my app. In PHP I would have handled this by testing the $_SERVER["HTTP_HOST"] variable to find out what server I am on, and adjust accordingly. However, I'm not as familiar with Java and am wondering if it has a similar method? Or if maybe even JavaScript had a similar method?

代码如下:

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中我会这样做:

In PHP I would have just done this:

var server = <?= $_SERVER["HTTP_HOST"] ?>
var url = "http://" + server + "/store/results/index.php";
window.location = url;

任何想法?我想我的运作是假设您必须使用绝对URL来更改JavaScript中当前窗口的位置。如果还有其他方法可以在没有绝对URL的情况下更改JavaScript中的窗口位置,请随时提供。

Any ideas? I suppose I'm operating under the assumption that you have to do an absolute URL to change the location of the current window in JavaScript. If there is another way to change the window location in JavaScript without an absolute URL, please feel free to offer that as well.

提前致谢...

推荐答案

您需要的是:

request.getServerName()

例如:

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

这篇关于JSP或JavaScript等效于PHP的$ _SERVER [&quot; HTTP_HOST&quot;]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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