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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM