JavaWeb中的絕對路徑和相對路徑問題,javaweb絕對路徑

來源:互聯網
上載者:User

JavaWeb中的絕對路徑和相對路徑問題,javaweb絕對路徑

在開發的時候,因為路徑,往往會造成很多問題。所以開發時,JavaWeb儘可能的用絕對路徑

a.jsp

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong><body><h1>A page</h1><a href="pathProblem/b.jsp">To B page</a></body></strong></span>
b.jsp

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong><body><h1>B page</h1>Citys:<%= request.getAttribute("citys") %><a href="c.jsp">To C page</a></body></strong></span>
c.jsp

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong><body><h1>C page</h1><a href="../a.jsp">To A page</a></body></strong></span>

上面3個jsp,都是相對路徑,可以正常訪問

但是,通過轉寄和重新導向後

a.jsp

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong><body><h1>A page</h1><a href="testServlet">To B page</a></body></strong></span>
b.jsp

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong><body><h1>B page</h1>Citys:<%= request.getAttribute("citys") %><a href="c.jsp">To C page</a></body></strong></span>
c.jsp

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong><body><h1>C page</h1><a href="../a.jsp">To A page</a></body></strong></span>

testServlet

<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>@WebServlet("/testServlet")public class testServlet extends HttpServlet {private static final long serialVersionUID = 1L;       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//1.類比從資料庫中查詢資料List<String> citys = Arrays.asList("BJ","SH","GZ");request.setAttribute("citys", citys);// 根目錄下pathProblem 目錄 下的b.jsprequest.getRequestDispatcher("/pathProblem/b.jsp").forward(request, response);}}</strong></span>

a.jsp 可以訪問 b.jsp,但是b.jsp卻不能訪問c.jsp?

因為在 a.jsp 轉寄到 b.jsp後,路徑( http://localhost:8080/Session/testServlet ),從b.jsp 再去c.jsp時,因為 b->c 是相對路徑,所以 b 會認為 c.jsp 和當前WEB應用的根目錄 ( /Session)處於同一目錄,找不到,所以就會訪問失敗。這就出現了路徑錯亂的問題。



 總結:



1.絕對路徑的問題
1). 開發時建議編寫 "絕對路徑" :寫絕對路徑肯定不會出問題,但是寫相對路徑有時候就會出現問題


在由 Servlet 轉寄到 JSP 頁面時,此時瀏覽器地址欄上顯示的是Servlet 的路徑,而若 JSP 的頁面的超連結還是相對於該 JSP
頁面的地址,則可能會出現路徑混亂的問題.

根目錄
     -/a.jsp 
     -path

-/b.jsp

-/c.jsp

a.jsp -> Servlet 轉寄 -> b.jsp (有超連結,串連到和b.jsp 同目錄下c.jsp 這時就會出現問題)->頁面混亂
 
2).編寫絕對路徑可以避免上述問題:

絕對路徑: 相對當前 Web 應用程式的根路徑(ContextPath)的路徑,即任何路徑都必須帶上ContextPath.
http://localhost:8080/Session/a.jsp √.   ContextPath -> Session
http://localhost:8080/a.jsp         ×.   當前網站  ->  http://localhost:8080/


3).JavaWeb 中的 '/' 到底代表什麼?

①.有時候代表的是: 當前WEB應用的根路徑(http://localhost:8080/ContextPath/) :若 '/' 需要交給Servlet 來處理

>請求轉寄時,request.getRequestDispatcher("/xxxx").forward(,,);
>web.xml 中 映射Servlet
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern> /TestServlet </url-pattern>
</servlet-mapping>
>各種定製標籤中的 / 
②.有時代表的是當前WEB網站的根路徑(http://localhost:8080/):若 '/' 交給瀏覽器處理

>超連結:<a href="/XXXX">
>表單中的action <form action="/login.jsp">
>做請求重新導向的時候,response.sendRedirect("/a.jsp");

如何完成編寫絕對路徑:

若'/'代表的是網站的根目錄,在其前面加上ContextPath 就可以了,而ContextPath 可以由request.getContextPath() 或
application.getContextPath() 得到
<a href="/TestPath">B page</a> 就無法訪問Servlet
改為 <a href="<%=request.getContextPath()%>/TestPath"> B page </a>
response.sendRedirect(request.getContextPath()+"/pathProblem/b.jsp");


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.