Skip to content Skip to sidebar Skip to footer

JSP Or JavaScript Equivalent To PHP's $_SERVER["HTTP_HOST"]?

I've go an absolute URL in my JavaScript that I have hard coded for window.location. I don't want to have to change this every time I am testing my app. In PHP I would have handled

Solution 1:

What you need is:

request.getServerName()

An example:

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

Solution 2:

Javascript:

var server = window.location.hostname;

Solution 3:

The location object has several properties, and the one you'd want is hostname.

Or, you can optionally just use a root-relative URL and just set the pathname property and not mess with the host business at all!

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

Solution 4:

You really should have search for this but in JSP it's :

request.getRemoteHost()

Post a Comment for "JSP Or JavaScript Equivalent To PHP's $_SERVER["HTTP_HOST"]?"