一.比如我先給出一個完整的訪問地址:
http://localhost:8080/Demo01/AServlet?username="xxx"&passwd="XXX"
(1)協議名是http,獲取協議名的方法:
request.getScheme();
(2)服務器名是localhotst,獲取服務器名的方法:
request.getServerName();
(3)服務器接口是8080,獲取服務器接口方法:
request.getServerPort();
(4)項目名是Demo01,獲取項目名方法:
request.getContextPath();
(5)Servlet路徑是/AServlet,獲取方法:
request.getServletPath();
(6)參數部分是username="xxx"&passwd="XXX",獲取方法:
request.getQueryString();
(7)URI是/Demo01/AServlet,獲取方法:
request.getRequestURI();
(8)URL是http://localhost:8080/Demo01/AServlet,獲取方法
request.getRequestURL();
(8)如果要獲取webapp的路徑:request.getSession().getServletContext().getRealPath("/")
二.示意圖