在學習javaweb時,我們可能經常會被這個問題難倒:相同的路徑,轉發後出現 404 錯誤,而重定向則不會。
對於這個問題,我進行了多次反覆的測試,總結出了一下知識點:
路徑有相對路徑和絕對路徑———當前路徑爲 http://localhost:8080/Test/Main :
相對路徑:response.sendRedirect("index.jsp")------htttp://localhost:8080/Test/Main/index.jsp
request.getRequestDispatcher("index.jsp")------htttp://localhost:8080/Test/Main/index.jsp
絕對路徑:response.sendRedirect("/index.jsp")------htttp://localhost:8080/index.jsp
request.getRequestDispatcher("/index.jsp")------htttp://localhost:8080/Test/index.jsp