在学习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
本文通过实例对比了JavaWeb中转发与重定向的区别,详细解释了相对路径与绝对路径的使用场景及其对页面跳转的影响。
2315

被折叠的 条评论
为什么被折叠?



