getContextPath、getServletPath、getRequestURI、getRealPath、getRequestURL、getPathInfo();的區別


<%  
    out.println("getContextPath: "+request.getContextPath()+"<br/>");  
    out.println("getServletPath: "+request.getServletPath()+"<br/>");  
    out.println("getRealPath: "+request.getRealPath("/")+"<br/>");  
    out.println("getRequestURL: "+request.getRequestURL()+"<br/>");  
    out.println("getRequestURI: "+request.getRequestURI()+"<br/>");  
  
%>

 

出來的輸出效果為:

getContextPath: /ckswzl  
getServletPath: /admin/login.jsp  
getRealPath: D:\document\EclipseWorkSpace2\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ckswzl\  
getRequestURL: http://localhost:8080/ckswzl/admin/login.jsp  
getRequestURI: /ckswzl/admin/login.jsp  

 

getContextPath是返回的項目上下文的名字(其實也就是項目名);

getServletPath是返回的是項目名到當前jsp文件的路徑(意思就是在這個項目首頁到文件的路徑)

getRequestURI是返回的是項目名到整個文件的請求路徑

getRealPath是返回的文件所在的絕對路勁。相對於當前計算機的真實路徑

getRequestURL是返回的整個URL的路徑請求(意思就是返回的瀏覽器地址欄的整個地址)

request.getPathInfo();這個方法返回請求的實際URL相對於請求的serlvet的url的路徑。

 

 

如果我們的 servlet-mapping 如下配置: 

<servlet-mapping>
  <servlet-name>jetbrick-template</servlet-name>
  <url-pattern>/template/*</url-pattern>
</servlet-mapping>

 

 

 

那么訪問: /context/templates/index.jetx

request.getServletPath() == "/templates"
request.getPathInfo() == "/index.jetx"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM