java 或者 js 獲取項目訪問路徑(域名)


/**
 * 獲得站點url
 * @return
 */
public String getWebUrl(){
  String url = getRequest().getScheme() + "://" + getRequest().getServerName();
  if(getRequest().getServerPort()!=80){
    url+= ":" + getRequest().getServerPort();
  }
  url+=getRequest().getContextPath();
  return url;
}

 

js

/**
 * 獲取項目跟路徑,http://localhost:8080/userCenter
 * @returns
 */
function getContextPath() {
   //獲取當前網址,如: 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);
}

 


免責聲明!

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



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