request如何獲取請求路徑方法
阿新 • • 發佈:2019-02-12
request物件中包含的是請求資訊,當我們在瀏覽器位址列上輸入:
http://localhost:8080/Example/AServlet?username=zhangsan,這段地址也會作為請求資訊封裝在request物件中,request物件必然會提供相關的方法來獲取這些請求資訊,這些資訊其實就是請求路徑資訊。
request物件通過以下方法來獲取請求路徑,如下所示:
- String getServerName():獲取伺服器名,localhost;
- String getServerPort():獲取伺服器埠號,8080;
- String getContextPath():獲取專案名,/Example;
-
String getServletPath():獲取
- String getQueryString():獲取引數部分,即問號後面的部分:username=zhangsan
- String getRequestURI():獲取請求URI,等於專案名+Servlet路徑:/Example/AServlet
-
String getRequestURL():獲取請求URL,等於不包含引數的整個請求路徑:http://localhost:8080/Example/AServlet 。