servlet和JS獲取域名和項目

	public String getHeadUrl(HttpServletRequest request) {
		String projName=request.getContextPath();
		String netPort=request.getRequestURL().toString().split("//")[1].split("/")[0];
		String netAddr="http://"+netPort;
		return netAddr+projName;
	}

  

 

JS:

  //獲取當前網址,如:http://127.0.0.1:8080/Tmall/index.jsp?id=1
var curWwwPath=window.document.location.href;

//獲取主機地址之後的目錄如:/Tmall/index.jsp
var pathName=window.document.location.pathname;
var pos=curWwwPath.indexOf(pathName);

//獲取主機地址,如:http://127.0.0.1:8080
var localhostPaht=curWwwPath.substring(0,pos);

//獲取帶"/"的項目名,如:/Tmall
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); 

 

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章