Servlet forwarding and redirection

Forwarding and Redirection

- Forwarding

Forwarding is after the browser sends a request to servlet1, servlet1 need access servlet2, therefore inside the server to jump to servlet2, sometimes also known as jump forward within the server. The whole process only once the browser makes a request, the server issues an response. So, whether it is servlet1 or servlet2, the whole process, there is only one request, that request submitted by the user. Thus servlet1 servlet2 and can be obtained from the request to the associated data when the user submits a request carried.
Example: You select the pair of basketball shoes in the store, the clerk did not tell you this pair of shoes now, and let you wait for the next, he went to another store to pick up, after the clerk to get your hands into the shoes shoes


Code Example:
Create html with a form of:


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<Title> form </ title>
</head>
<body>
    <form action="Forward01" method="post">
        用户名:<input name="username" type="text">
        

        密码:<input name="password" type="password">
        

        <input type="submit" value="提交">
    </form>
</body>
</html>



Create a servlet named Forward01 of:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Forwarding
*/
public class Forward01 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // set the character encoding
        request.setCharacterEncoding("UTF-8");
        // Get the request parameters
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        request.setAttribute("username", username);
        request.setAttribute("password", password);

        // forward
        request.getRequestDispatcher("Other").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}





创建名称为Other的servlet:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 转发
*/
public class Other extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = (String)request.getAttribute("username");
        String password = (String)request.getAttribute("password");

        System.out.println(username);
        System.out.println(password);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}


当点击之后注意浏览器地址栏中的url是不会发生变化的。

-- 重定向

重定向是浏览器发送请求到servlet1之后,servlet1需要访问servlet2,但并未在服务器内直接访问,而是由服务器自动向浏览器发送一个响应,浏览器再自动提交一个新的请求,这个请求就是对servlet2 的请求。
对于servlet2的访问,是先由服务器响应客户端浏览器,再由客户端浏览器向服务器发送对servlet2的请求,所以重定向有时又称为服务器外跳转。
整个过程中,浏览器共提交了两次请求,服务器共发送了两次响应。只不过,第一次响
应与第二次请求,对于用户来说是透明的,是感知不到的。用户认为,自己只提交了一次请
求,且只收到了一次响应。
这样的话,就会有一个问题:servlet2中是无法获取到用户手动提交请求中的数据的,它只能获取到第二次请求中所携带的数据。
举例:你在专卖店选中一双篮球鞋,店员告诉你这双鞋现在没有了,附近的另一个专卖店中有这双鞋,他把那个专卖店的地址告诉你,你得到地址之后,自己到达另外一个专卖店买到了这双鞋。




代码示例:
将上面的html表单中的action修改一下。
创建一个名为Redirect01的servlet:





import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 重定向
*/
public class Redirect01 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符编码
        request.setCharacterEncoding("UTF-8");
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        request.setAttribute("username", username);
        request.setAttribute("password", password);

        //重定向到上面的Other servlet中
        response.sendRedirect("Other");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}



当点击之后注意浏览器地址栏中的url是会发生变化的,url后面请求的路径变为了Other。
如果想要重定向到另外一个项目的servlet上时,只需要在sendRedirect加上项目的访问名:
response.sendRedirect("/other-app/Other");
其中other-app是另外项目的访问名。


-- 转发和重定向的区别

  • 请求转发
  1. 浏览器只发出一次请求,收到一次响应
  2. 请求所转发到的servlet2中可以直接获取到请求中所携带的数据
  3. 浏览器地址栏显示的为用户所提交的请求路径
  4. 只能跳转到当前应用的资源中
重定向
  1. 浏览器发出两次请求,接收到两次响应
  2. 重定向到的servlet2不能直接获取到用户提交请求中所携带的数据
  3. 浏览器地址栏显示的为重定向的请求路径,而非用户提交请求的路径。也正因为如此,重定向的一个很重要作用是:防止表单重复提交
  4. 重定向不仅可以跳转到当前应用的其它资源,也可以跳转到到其它应用中资源

-- 请求转发与重定向的选择

  • 若需要跳转到其它应用,则使用重定向。
  • 若是处理表单数据的Servlet1要跳转到另外的Servlet2上,则需要选择重定向。为了防止表单重复提交。
  • 若对某一请求进行处理的 Servlet 的执行需要消耗大量的服务器资源(CPU、内存),此时这个 Servlet 执行完毕后,也需要重定向。
  • 其它情况,一般使用请求转发。




发布了15 篇原创文章 · 获赞 39 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_37024565/article/details/80693407