結論:開發時候建議編寫絕對路徑(以"/"開頭的路徑),相對路徑可能會有問題。
何爲絕對路徑? 帶有ContextPath的路徑,通常情況下ContextPath=/工程名
獲取ContextPath? request.getContextPath();
路徑中[ / ]代表的含義
①WEB應用的根路徑:http://localhost:8080/ContextPath/
>請求轉發:request.getRequestDispatcher(path).forward(request, response);
>web.xml文件映射Servlet訪問路徑
<servlet-mapping>
<servlet-name>ForwordServlet</servlet-name>
<url-pattern>/forwordServlet</url-pattern>
</servlet-mapping>
>各種定製標籤
②WEB站點的根路徑:http://localhost:8080
例如:http://localhost:8080/cookiesession/session/session2/login.jsp
>超鏈接:<a href="login.jsp" >切換賬號</a>
==><a href="<%= request.getContextPath() %>/session/session2/login.jsp" >切換賬號</a>
>表單提交中的 action:action=hello.jsp
>請求重定向的時候:response.sendRedirect(path);