筆記——重定向與請求分派

重定向

例:http://localhost:8080/Test/MyJsp.jsp

請求URL:http://localhost:8080/Test/

相對路徑,無”/”(容器會相對於原先的請求URL建立完整的URL), 如請求上述URL

Response.sendRedirect(“test.jsp”);

絕對路徑,有”/”(容器會相對於Web應用本身建立完整的URL,如

response.sendRedirect("/Test/MyJsp.jsp");

ps:不能寫在響應之後再調用sendRedirect()

PrintWriter writer =response.getWriter();
writer.write("hh");
writer.flush();
response.sendRedirect("/Test/MyJsp.jsp");

 

請求分派(頁面轉發)  (地址欄沒有變化)

這樣頁面跳轉後請求仍是頁面轉發前的那個請求(轉發時可對請求進行修改)

RequestDispatcherview = request.getRequestDispatcher(“MyJsp.jsp”);
View.forward(request,response);

 

發佈了33 篇原創文章 · 獲贊 8 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章