request對象中包含的是請求信息,當咱們在瀏覽器地址欄上輸入:
http://localhost:8080/Example/AServlet?username=zhangsan,這段地址也會做爲請求信息封裝在request對象中,request對象必然會提供相關的方法來獲取這些請求信息,這些信息其實就是請求路徑信息。
request對象經過如下方法來獲取請求路徑,以下所示:
String getServerName():獲取服務器名,localhost;
String getServerPort():獲取服務器端口號,8080;
String getContextPath():獲取項目名,/Example;
String getServletPath():獲取Servlet路徑,/AServlet;
String getQueryString():獲取參數部分,即問號後面的部分:username=zhangsan
String getRequestURI():獲取請求URI,等於項目名+Servlet路徑:/Example/AServlet
String getRequestURL():獲取請求URL,等於不包含參數的整個請求路徑:http://localhost:8080/Example/AServlet
String request.getRemoteAddr():獲取服務器的IP,如localhost對應ip爲127.0.0.1瀏覽器