zoukankan      html  css  js  c++  java
  • java ,js获取web工程路径

    一、java获取web工程路径

           1),在servlet可以用一下方法取得:

      request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/”) ”//upload//”;

      2),不从jsp,或servlet中获取,只从普通java类中获取:

      String path =

      getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

      SAXReader() saxReader = new SAXReader();

      if(path.indexOf(“WEB-INF”)>0){

      path = path.substring(0,path.indexOf(“/WEB-INF/classes”) 16);

      // ‘/WEB-INF/classes’为16位

      document = saxReader.read(path filename);

      }else{

      document = saxReader.read(getClass().getResourceAsStream(filename));

      }

      weblogic tomcat 下都有效

      String path =

      getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

      <!--EndFragment-->

    <%
    //此方法是通用的,可以获取不同系统中应用项目的根目录
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +  path + "/";
    %>

    二、js获取web工程路径

           var strFullPath=window.document.location.href;
             var strPath=window.document.location.pathname;
             var pos=strFullPath.indexOf(strPath);
             var prePath=strFullPath.substring(0,pos);
             var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
             var webPath=prePath+postPath;

  • 相关阅读:
    太久了
    idea文件过大解决方法
    ibus词库导入
    Linux认证考试
    Idea Linux 下的配置 max_user_watches
    SpringBoot启动过程之web,servlet/filter等
    SpringBoot启动过程学习
    也谈一下面试
    MVCC
    Python pyside2 qt 画画面
  • 原文地址:https://www.cnblogs.com/tancp/p/3848432.html
Copyright © 2011-2022 走看看