很多朋友都想知道java如何獲取請求域名?下面就一起來了解一下吧~
1、獲取協議名和域名。
request.getScheme();?//得到協議名?例如:http request.getServerName();?//得到域名?localhost
2、獲取全路徑。
request.getRequestURL();?//得到http://localhost:8888/CRM/loginController/login
3、獲取請求所有參數 //map類型。
request.getParameterMap()
4、獲取項目名
request.getContextPath();?//?/CRM
5、獲取請求方法
request.getServletPath();?//?/loginController/login
/** ?????*?獲取當前訪問URL?(含協議、域名、端口號[忽略80端口]、項目名) ?????*?@param?request ?????*?@return:?String ?????*/ ????public?static?String?getServerUrl(HttpServletRequest?request)?{ ????????//?訪問協議 ????????String?agreement?=?request.getScheme(); ????????//?訪問域名 ????????String?serverName?=?request.getServerName(); ????????//?訪問端口號 ????????int?port?=?request.getServerPort(); ????????//?訪問項目名 ????????String?contextPath?=?request.getContextPath(); ????????String?url?=?"%s://%s%s%s"; ????????String?portStr?=?""; ????????if?(port?!=?80)?{ ????????????portStr?+=?":"?+?port; ????????} ????????return?String.format(url,?agreement,?serverName,?portStr,?contextPath); ????}
以上就是小編今天的分享,希望能幫到大家。