weidagang2046的專欄

          物格而后知致
          隨筆 - 8, 文章 - 409, 評論 - 101, 引用 - 0
          數(shù)據(jù)加載中……

          What's the difference between "redirect" & "forward" in servelt?

          Suppose you have a servlet that just does another redirect to another URL:

          protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)
          {
            response.sendRedirect("/anotherURL");
          }


          This servlet is accessible through the URL /redirectServlet.

          When the client browser makes a request for /redirectServlet, it receives a response (an HTTP 302 response) that tells it that the document it requested can actually be found at /anotherURL. The client uses the URL it gets and makes a new request to /anotherURL.

          The redirect URL doesn't have to be on the same server as the original URL. If the redirect URL (/anotherURL) points to another servlet, new request and response objects will be created to handle the new request.

          Suppose our redirect servlet is now changed to do a forward instead of a redirect:

          protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)
          {
            ServletContext context = getServletContext();
            RequestDispatcher dispatcher = context.getRequestDispatcher("/anotherURL");
            dispatcher.forward(request, response);
          }


          This modified servlet is accessible through the URL /forwardServlet.

          When the client makes a request to our forwarding servlet this time, it no longer receives the redirect response. The dispatcher.forward line causes the servlet container to run the servlet associated with the url /anotherURL directly. The client will receive the result of /anotherURL, even though they requested /forwardServlet.

          A few more notes:

          * Redirect is a two step process. The web server tells the browser to request a second URL.
          * If the user reloads, the second URL will be reloaded (/anotherURL instead of /redirectServlet).
          * Redirect is always slower than a forward because it requires a second client request.
          * Attributes placed in the request scope of the redirecting servlet are not available in the request scope of the second rrequest.

          * Forwards are performed internally to the web server.
          * The browser is completely unaware that the forward took place - the original URL (/forwardServlet) remains in the address bar.
          * If the user does a reload, the browser will repeat the original request with the original URL (/forwardServlet).

          from: http://forum.java.sun.com/thread.jspa?threadID=627801&messageID=3597659

          posted on 2006-11-03 02:19 weidagang2046 閱讀(522) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 金华市| 美姑县| 上蔡县| 郯城县| 黄山市| 甘泉县| 枣强县| 太仓市| 石城县| 奉新县| 广安市| 宜州市| 张家界市| 滦南县| 揭阳市| 九龙县| 泰和县| 晋中市| 杭锦后旗| 子长县| 汕头市| 文水县| 沐川县| 辽宁省| 谷城县| 涞源县| 蕲春县| 绥德县| 曲阜市| 昭苏县| 巴林右旗| 鹤庆县| 南开区| 台州市| 瑞金市| 来凤县| 高青县| 昌邑市| 伊春市| 乌拉特中旗| 兴义市|