
請(qǐng)求重定向由客戶重新發(fā)起請(qǐng)求
請(qǐng)求分派有服務(wù)器自己內(nèi)部分派請(qǐng)求給另一內(nèi)容處理
重定向
servlet中重定向方法:
response.sendRedirect(Java.lang.String location);
jsp中實(shí)現(xiàn)重定向:
<%response.sendRedirect("new.jsp"); %> //重定向到new.jsp
原理:
重定向與客戶端交互,服務(wù)器將傳遞進(jìn)來(lái)的location直接相應(yīng)到客戶端,會(huì)產(chǎn)生302碼,瀏覽器自動(dòng)訪問(wèn)新的location。
特點(diǎn):
兩次請(qǐng)求和相應(yīng)URL地址發(fā)生了變化
數(shù)據(jù)會(huì)丟失,產(chǎn)生了兩個(gè)不同的HttpServletRequest對(duì)象
地址問(wèn)題:
重定向有絕對(duì)地址和相對(duì)地址之分
絕對(duì)地址不加“/”
response.sendRedirect("second.do");
相對(duì)地址加“/”
response.sendRedirect(request.getContextPath()+"/second.do");
請(qǐng)求分派
servlet中實(shí)現(xiàn)請(qǐng)求分派:
RequestDispatcher rd=request.getRequestDispatcher("second.do");
rd.forward(request, response);
jsp實(shí)現(xiàn)請(qǐng)求分派:
<jsp:forward page="apage.jsp" />
原理:
發(fā)生在服務(wù)器端,一個(gè)servlet對(duì)象調(diào)用另外一個(gè)servlet對(duì)象,會(huì)將request和response傳遞過(guò)去;
特點(diǎn):
只有一次請(qǐng)求和響應(yīng)
URL地址不會(huì)改變
數(shù)據(jù)不會(huì)丟失