Servlet的Request中和用戶請(qǐng)求路徑相關(guān)的操作有多個(gè),下面一一列舉:
1.getQueryString(),&之后GET方法的參數(shù)部分。
2.getServletPath(),web.xml中定義的Servlet訪問(wèn)路徑。
3.getPathInfo(),Servlet訪問(wèn)路徑之后,QueryString之前的中間部分。
4.getContextPath(),Context路徑前綴(這個(gè)應(yīng)該是最熟悉的啦)。
5.getRequestURI(),等于getContextPath() + getServletPath() + getPathInfo()。
6.getRequestURL(),等于getScheme() + "://" + getServerName() + ":" + getServerPort() + getRequestURI()。
7.getPathTranslated(),等于getServletContext().getRealPath("/") + getPathInfo()。
注意:
1.getServletContext().getRealPath("/") 后包含當(dāng)前系統(tǒng)的文件夾分隔符(windows系統(tǒng)是"\",linux系統(tǒng)是"/"),而getPathInfo()以"/"開頭。
2.getPathInfo()與getPathTranslated()在servlet的url-pattern被設(shè)置為/*或/aa/*之類的pattern時(shí)才有值,其他時(shí)候都返回null。
3.在servlet的url-pattern被設(shè)置為*.xx之類的pattern時(shí),getServletPath()返回的是getRequestURI()去掉前面ContextPath的剩余部分。