美文网首页
请求转发和请求包含

请求转发和请求包含

作者: 丫甘九 | 来源:发表于2019-04-08 19:56 被阅读0次

请求转发和请求包含

RequestDispatcher rd = request.getRequestDispatcher("/AServlet");

请求转发
rd.forward(request,reponse);
请求包含
rd.include(request,reponse);

有时一个请求需要多个Servlet协作才能完成,所以需要在一个Servlet跳到另一个Servlet

  • 一个请求跨多个Servlet,需要使用转发和包含
  • 请求转发:由下一个Servlet完成响应体,当前Servlet可以设置响应头!(留头不留体)
  • 请求包含:由俩个Servlet共同完成响应体(留头又留体)
  • 无论是请求转发还是请求包含,都在一个请求范围内!使用同一个request和同一个reponse


    image.png

演示请求转发

package bao;

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

@WebServlet(name = "oneServlet")
public class oneServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException {
        System.out.println("OneServlet...");
        /*设置响应头*/
        response.setHeader("aaa","AAA");
        /*设置响应体*/
        response.getWriter().print("Hello!oneServlet");
        /*请求转发*/
        request.getRequestDispatcher("twoServlet").forward(request,response);

    }

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

    }
}

package bao;

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

@WebServlet(name = "twoServlet")
public class twoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException {
        System.out.println("twoServlet....");
        /*设置响应体*/
        response.getWriter().print("Hello!twoServlet");

    }

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

    }
}

编译有错误

request域

void setAttribute(String name,Object value)
Object getAttribute(String name,Object value)

  • 同一请求范围内使用request.setAttribute(),request.getAttribute()来传值,前一个Servlet调用setAttribute()来保存值,后一个Servlet调用getAttribute()写值

演示请求包含

package bao;

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

@WebServlet(name = "oneServlet")
public class oneServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException {
        System.out.println("OneServlet...");
        /*设置响应头*/
        response.setHeader("aaa","AAA");
        /*设置响应体*/
        response.getWriter().print("aaaaaa");
        /*请求包含*/
        request.getRequestDispatcher("twoServlet").include(request,response);

    }

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

    }
}


package bao;

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

@WebServlet(name = "twoServlet")
public class twoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException {
        System.out.println("twoServlet....");
        /*设置响应体*/
        response.getWriter().print("Hello!twoServlet");

    }

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

    }
}

编译有错误

请求转发和重定向的区别

  • 请求转发是一个请求一次响应,而重定向是俩次请求俩次响应
  • 请求转发地址栏不变化,而重定向会显示后一个请求的地址
  • 请求转发只能转发到本项目其他Servlet,而重定向不止能重定向到本项目其他Servlet,还能定向到其他项目
  • 请求转发是服务端行为,只需给出转发的Servlet路径,而重定向需要给出requestURL路径,即包含项目名
    -请求转发和重定向是转发效率高,因为只有一次请求
  • 需要地址栏发生变化,那么必须使用重定向
  • 需要在下一个Servlet中获取request域中的数据,必须使用转发

相关文章

网友评论

      本文标题:请求转发和请求包含

      本文链接:https://www.haomeiwen.com/subject/ancpiqtx.html