JSP or JavaScript equivalent to PHP's $_SERVER[“HTTP_HOST”]?

前端 未结 4 1187
执念已碎
执念已碎 2021-01-02 22:22

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 w

相关标签:
4条回答
  • 2021-01-02 22:42

    Javascript:

    var server = window.location.hostname;
    
    0 讨论(0)
  • 2021-01-02 22:54

    What you need is:

    request.getServerName()
    

    An example:

    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    0 讨论(0)
  • 2021-01-02 22:56

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

    request.getRemoteHost()
    
    0 讨论(0)
  • 2021-01-02 23:00

    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";
    
    0 讨论(0)
提交回复
热议问题