例如:
在瀏覽器地址欄輸入 http://localhost:8080/ajax01_2/regist.jsp
注冊(cè)頁面,用戶名填寫后,觸發(fā)onblur 事件,
使 ajax 發(fā)送了get 請(qǐng)求:
check_username.do?username=' + document.getElementById('username')
【Servlet 中,繼承自父類的方法,可以直接調(diào)用
(也可以通過 this 關(guān)鍵字調(diào)用從父類繼承過來的該方法)】
String servletName = getServletName();
ServletConfig servletConfig = getServletConfig(); -- 下面有用到
ServletContext servletContext = getServletContext(); -- 下面有用到
【在 Servlet 中的各種路徑】:
【#案例】
ajax01_2/src/web/Path2ObtainServlet.java
request.getContextPath() --
返回 -- /ajax01_2
當(dāng)前請(qǐng)求資源路徑中的“應(yīng)用名”:
request.getServletPath()
返回 -- /check_username.do
訪問 Servlet 的請(qǐng)求資源路徑(不包含應(yīng)用名)
(請(qǐng)求資源路徑中,在應(yīng)用名之后的路徑)
request.getRequestURI()
返回 -- /ajax01_2/check_username.do
訪問 Servlet 的請(qǐng)求資源路徑(包含應(yīng)用名)
(請(qǐng)求資源路徑中,從應(yīng)用名開始的路徑)
由上面上段內(nèi)容可知:
request.getRequestURI() = request.getContextPath() + request.getServletPath()
--------------------------------------------------------------------------------
【注意:
ServletContext application = this.getServletContext();
-- 此處的 application 就是 JSP 中的隱含對(duì)象】
getServletContext().getRealPath(request.getRequestURI())
【或 request.getSession().getServletContext().getRealPath(request.getRequestURI()); 】
返回 -- D:\apache-tomcat-6.0.37\webapps\ajax01_2\ajax01_2\check_username.do
訪問 Servlet 的請(qǐng)求資源路徑,在硬盤中完整的絕對(duì)路徑。
getServletContext().getRealPath('/')
【或 getServletConfig().getServletContext().getRealPath('/'); 】
返回 -- D:\apache-tomcat-6.0.37\webapps\ajax01_2\check_username.do
例如:
在瀏覽器地址欄輸入 http://localhost:8080/ajax01_2/RootingProblem/JSPPath2Obtain.jsp
【jsp 中各種路徑的獲取】
request.getContextPath()
表示: 當(dāng)前 jsp 文件的上下文路徑 (即獲取請(qǐng)求資源路徑的應(yīng)用名)
返回: /ajax01_2
request.getServletPath()
表示: 獲請(qǐng)求資源路徑中,應(yīng)用名之后的路徑
返回: /RootingProblem/JSPPath2Obtain.jsp
request.getRequestURI()
表示: 當(dāng)前 jsp 文件,在當(dāng)前應(yīng)用根目錄中的絕對(duì)路徑
(即獲取請(qǐng)求資源路徑中,從應(yīng)用名開始的路徑)
返回: /ajax01_2/RootingProblem/JSPPath2Obtain.jsp
由上面上段內(nèi)容可知:
request.getRequestURI() = request.getContextPath() + request.getServletPath()
---------------------------------------------------------------------------------
application.getRealPath(request.getRequestURI())
表示: 當(dāng)前 jsp 文件的絕對(duì)路徑。(即獲取請(qǐng)求文件在硬盤中的路徑)
返回:D:\vidio\Tarena JSD_1306\2013-09-25_changeToNoteBook\apache-tomcat-6.0.37\webapps\ajax01_2\ajax01_2\RootingProblem\JSPPath2Obtain.jsp
application.getRealPath("/")
表示: 獲取請(qǐng)求文件所在工程,在硬盤中的完整路徑:
返回: D:\vidio\Tarena JSD_1306\2013-09-25_changeToNoteBook\apache-tomcat-6.0.37\webapps\ajax01_2\