jsp中的request.getContextPath()

來源:互聯網
上載者:User

標籤:9.png   伺服器   tomcat   span   為什麼   images   代碼   gif   web   

jsp中的 request.getContextPath() 

   <%=request.getContextPath()%>是為瞭解決相對路徑的問題,可返回網站的根路徑。

但不用也可以吧,比如<a href="<%=request.getContextPath()%>/catalog.jsp">
我就直接用<a href="catalog.jsp">也行啊,這兩個檔案是在同一個目錄下的

比如你要產生一個檔案放在伺服器上得一個目錄下,你可以使用request.getContextPath()+/dir,組成一個完整得目錄結構!

但在JSP檔案裡,通過request.getContextPath()得到的路徑卻為空白,為什麼?

 

 

你在context中沒有配置path屬性,所以你的工程檔案就是在根目錄下,相當於path="";即是你直接在 瀏覽器中輸入你的伺服器ip就會到你的jsp頁面,而不是tomcat的預設頁面;所以你通過request.getContextPath()得到的字 符串是為空白的;它是獲得虛目錄的;
如果你想得到工程檔案的實際實體路徑,可通過:<%=request.getRealPath("/")%>,這樣頁面就會輸出:d:/web

request.getScheme();
返回的協議名稱,預設是http

request.getServerName()
返回的是你瀏覽器中顯示的主機名稱,你自己試一下就知道了

getServerPort()
擷取伺服器連接埠號碼

 

 


 

假定你的web application 名稱為news,你在瀏覽器中輸入請求路徑:

http://localhost:8080/news/main/list.jsp

則執行下面向行代碼後列印出如下結果:

1、 System.out.println(request.getContextPath());

列印結果:/news
   2、System.out.println(request.getServletPath());

列印結果:/main/list.jsp
3、 System.out.println(request.getRequestURI());

列印結果:/news/main/list.jsp
4、 System.out.println(request.getRealPath("/"));

列印結果:F:\Tomcat 6.0\webapps\news\test

       

jsp中的request.getContextPath()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.