JSP or JavaScript equivalent to PHP $ _SERVER ["HTTP_HOST"]?

I use an absolute URL in my JavaScript, which I hardcoded for window.location.

I do not want to change this every time I test my application. In PHP, I would handle this by testing the $ _SERVER ["HTTP_HOST"] variable to find out which server I’m on and adjust accordingly. However, I'm not so familiar with Java, and I wonder if it has a similar method? Or maybe even JavaScript had a similar method?

The code is as follows:

var url = "http://172.17.1.107/store/results/index.jsp";
window.location = url;

What I would like to do:

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;

In PHP, I would just do this:

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

? , , URL-, JavaScript. JavaScript URL-, , .

...

+5
5

:

request.getServerName()

:

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

, , , hostname.

URL- pathname, - !

location.pathname = "/store/results/index.jsp";
+4

JavaScript:

var server = window.location.hostname;
+2

You really need to look for this, but in JSP it is:

request.getRemoteHost()
-1
source

Perhaps this may help.

It will replace any words you want, or nothing. It will also work for your request.

var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools"); 
-3
source

All Articles