一个Web组件(Servlet/JSP)将未完成的处理通过容器转交给另外一个Web组件继续处理,转发 的各个组件会共享Request和Response对象。
创建空工程,在工程中创建javaEE模块
配置中设置tomcat的部署
编写register界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>转发测试</title>
</head>
<body>
<form action="forwardServlet" method="post">
<input type="submit" value="转发">
</form>
</body>
</html>
编写servlet
ForwardServlet
package com.example.forward_demo03;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "ForwardServlet", urlPatterns = "/forwardServlet")
public class ForwardServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("接收到了转发请求!");
//获取另一个web组件,让它进行转发
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/targetServlet");
requestDispatcher.forward(request, response);
}
}
这里看到,需要再编写一个 targetServlet,处理转发请求。它会执行真正的转发请求。
targetServlet
package com.example.forward_demo03;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "TargetServlet", value = "/targetServlet")
public class TargetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("接收到了传递过来的转发请求!");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.write("<h1>" + "转发成功!" + "</h1>");
}
}
部署测试:
运行tomcat
访问转发界面,发送转发请求
转发请求发送,浏览器界面显示
控制台输出显示
F12查看调试信息
可以看到只进行一次请求。
修改ForwardServlet
package com.example.forward_demo03;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "ForwardServlet", urlPatterns = "/forwardServlet")
public class ForwardServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("接收到了转发请求!");
request.setAttribute("key1", "value1");
//获取另一个web组件,让它进行转发
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/targetServlet");
requestDispatcher.forward(request, response);
}
}
修改TargetServlet
package com.example.forward_demo03;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "TargetServlet", value = "/targetServlet")
public class TargetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("接收到了传递过来的转发请求!");
Object value = request.getAttribute("key1");
System.out.println("获取到的转发来的key1对应的值:" + value);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.write("<h1>" + "转发成功!" + "</h1>");
}
}
测试结果
从这也可以看出,转发的request用的是同一个。
转发 | 重定向 | |
---|---|---|
转发/重定向之后浏览器地址栏的URL会不会发生改变 | 不会 | 会 |
转发/重定向过程中是否共享Request对象 | 是 | 否 |
转发/重定向的URL可不可以是其它项目工程 | 不可以 | 可以 |