Java Web中请求转发和请求包含

时间:2022-01-15 13:36:54

1、都是在一个请求中跨越多个Servlet

2、多个Servlet在一个请求中,他们共享request对象。就是在AServle中setAttribute()保存数据在BServlet中由getAttribute()取出数据

3、在AServlet转发到BServlet,那么整个请求都是由BServlet来完成,AServlet不再插手

4、AServlet 包含BServlet,那么两个Servlet共同完成请求

5、请求转发和请求包含都需要使用一个对象RequestDispatcher。

AServlet.java

package cn.edu.aynu.rjxy.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class AServlet extends HttpServlet {
/*
* 请求转发
* */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置响应头
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("AServlet...");
request.setAttribute("name", "张三"); //获得RequestDispatcher对象
RequestDispatcher rd = request.getRequestDispatcher("/BServlet");
//将request对象和response对象传递给BServlet
rd.forward(request, response); } }

BServlet.java

package cn.edu.aynu.rjxy.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class BServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = (String) request.getAttribute("name");
response.getWriter().print("BServlet为您服务...");
response.getWriter().print("name="+name);
} }

CServlet.java

package cn.edu.aynu.rjxy.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class CServlet extends HttpServlet { /*
* 请求包含
* */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置响应头
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("AServlet为您服务...");
RequestDispatcher rd = request.getRequestDispatcher("/DServlet");
rd.include(request, response);
} }

DSevler.java

package cn.edu.aynu.rjxy.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.getWriter().print("DServlet为您服务...");
} }

1、请求转发是一个请求,而重定向是两个请求

2、请求转发后浏览器地址栏不会发生变化重定向会发生变化

3、请求转发的目标只能在本项目中的资源,重定向的目标可以是其他应用

4、请求转发对Aservlet和Bservlet的请求方法是相同的,就是要么都是get,要么都是post。因为他们是同一个请求

5、请求转发可以在多个Servlet*享request的数据,而重定向不行。