HttpServletRequest對象獲取當前域名

獲取域名,如:http://f0rb.iteye.com/java

StringBuffer url = request.getRequestURL();
String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();

 

獲取帶部署環境上下文的域名,如: http://www.iteye.com/admin/app

  這個方法是在別人那看見的,我用到時候request.getServletContext()是報錯的;url

StringBuffer url = request.getRequestURL();
String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getServletContext().getContextPath()).append("/").toString();

  因而我看了下改爲了以下:spa

StringBuffer url = request.getRequestURL();
		String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getSession().getServletContext().getContextPath()).append("/").toString();
相關文章
相關標籤/搜索