【重定向和请求转发】

重定向的特点

1.两次请求两次转发
2.地址栏地址发生改变
3.不能在多个Servlet之间共享数据
4.几乎不受域的限制

请求转发的特点

1.一次请求一次转发
2.能在多个Servlet之间共享数据
3.只能在域之间使用

package web.response;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ResponseDemo03", value = "/ResponseDemo03")
public class ResponseDemo03 extends HttpServlet {
     
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
//        重定向sendRedirect()方法
        System.out.println("Demo03----------");
        response.sendRedirect("ResponseDemo02");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     

    }
}




```java
package web.servlet.Requwst;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RequestDemo05", value = "/RequestDemo05")
public class RequestDemo05 extends HttpServlet {
     
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
        System.out.println("5555555555");
//        数据共享
        request.setAttribute("name","东");
//        请求转发
        request.getRequestDispatcher("/RequestDemo06").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     

    }
}


你可能感兴趣的:(web)