SendRedirect in java servlet

結論: 直接用 sendRedirect 就好了.

Redirect

protected void doGet(HttpServletRequest req, HttpServletResponse resp){
    resp.sendRedirect("http://www.google.com");  
}

Forward

protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    RequestDispatcher dispatcher = getServletContext()
      .getRequestDispatcher("/servlet2");
    dispatcher.forward(req, resp);
}

forward() methodsendRedirect() method
The forward() method works at server side.The sendRedirect() method works at client side.
It sends the same request and response objects to another servlet.It always sends a new request.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *