获取项目名/项目路径

1.jsp 中

<%
String contextPath = request.getContextPath();
request.setAttribute("contextPath", contextPath);
%>

 http://localhost:8080/Info_System/user/user_info.jsp 

https://www.cnblogs.com/gzl180110/p/10754477.html

  1.request.getSchema();可以返回当前页面所使用的协议,就是”http”

  2.request.getServerName();返回当前页面所在服务器的名字,就是上面例子中的”localhost”

  3.request.getServerPort();返回当前页面所在服务器的端口号,就是上面例子中的”8080”

  4.request.getContextPath();返回当前页面所在的应用的名字,就是上面例子中的”Info_System”

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

根路径应该是那么http://localhost:80/Info_System/ 也就是 basePath

 2.js中 主要是基于    window.location

    1.window.document.location.href/window.document.location.pathname     

        function getRootPath_web() {
          //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
          var curWwwPath = window.document.location.href;
          //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
          var pathName = window.document.location.pathname;
          var pos = curWwwPath.indexOf(pathName);
          //获取主机地址,如: http://localhost:8083
          var localhostPaht = curWwwPath.substring(0, pos);
          //获取带"/"的项目名,如:/uimcardprj
          var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
          return (localhostPaht + projectName);
        }

    2.window.location.pathname/window.location.protocol/window.location.host

        function getRootPath_dc() {

          var pathName = window.location.pathname.substring(1);

          var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
          if (webName == "") {
            return window.location.protocol + '//' + window.location.host;
          }
          else {
            return window.location.protocol + '//' + window.location.host + '/' + webName;
          }
        }

    document默示的是一个文档对象,window默示的是一个窗口对象,一个窗口下可以有多个文档对象。
    所以一个窗口下只有一个window.location.href,然则可能有多个document.URL、document.location.href-

posted @ 2019-04-23 09:25  G_Lybbh  阅读(276)  评论(0编辑  收藏  举报