不同
请求重定向----------response.sendRedirect(String url)
- 两次请求,浏览器行为,地址栏改变,请求域中的数据会丢失
- 使用指定的重定向位置 URL 向客户端发送临时重定向响应并清除缓冲区。 缓冲区将被此方法设置的数据替换
- URL中的加"/“和不加”/"的区别
- 没有"/":根据当前页面的URL拼接上sendRedirect(String url)里面的URL。比如response.sendRedirect("/index.jsp"),其原先的路径为,即当前路径是http://localhost:8080/login/login.jsp,则会在URL—>http://localhost:8080/login的基础上,加上/index.jsp,就是路径则变为http://localhost:8080/login/index.jsp
- 有"/":则根据当前的web应用路径,即http://localhost:8080的基础,加上添加的路径,就比如当前路径是http://localhost:8080/login/login.jsp,重定向response.sendRedirect("/index.jsp"),
路径变为http://localhost:8080/index.jsp
- 当前的请求不跟着转发过去到重定向的URL对应的页面或Servlet
请求转发-------request.getRequestDispatcher(String url)
- 一次请求,服务器行为,地址栏不变,请求域中的数据不丢失
- 将当前请求,转发给对应的地方处理,请求Request对象是同一个
- 当前Servlet不输出其响应,只传递过去一个请求Request对象,输出其转发的那个响应。
- 代码演示
@WebServlet("/RequestDemo1")
public class RequestDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.拿到请求调度对象
RequestDispatcher rd = request.getRequestDispatcher("/RequestDemo2");//如果是给浏览器看的,/可写可不写。如果是给服务器看的,一般情况下,/都是必须的。
//放入数据到请求域中
request.setAttribute("CityCode", "bj-010");
//2.实现真正的转发操作
rd.forward(request, response);//实现真正的转发操作
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
@WebServlet("/RequestDemo2")
public class RequestDemo7 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取请求域中的数据
String value = (String)request.getAttribute("CityCode");
response.getWriter().write("welcome to request demo 2 "+value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
请求包含--------request.getRequestDispatcher(String url)
- 把两个Servlet的响应内容合并输出,将另外一个Servlet的响应,包含在当前页面输出
- 各编译各的,只是最后合并输出。只是将另一个Servlet的响应包含在当前Servlet输出,但单独访问哪个被包含的Servlt,还是按其原本的书写响应输出
- 代码示例
/**
* 请求包含
*
* 它是把两个Servlet的响应内容合并输出。
* 注意:
* 这种包含是动态包含。
*
* 动态包含的特点:
* 各编译各的,只是最后合并输出。
* @author 黑马程序员
* @Company http://www.itheima.com
*/
@WebServlet("/RequestDemo1")
public class RequestDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("I am request demo1 ");
//1.拿到请求调度对象
RequestDispatcher rd = request.getRequestDispatcher("/RequestDemo2");
//2.实现包含的操作
rd.include(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
/**
* 被包含者
* @author 黑马程序员
* @Company http://www.itheima.com
*/
@WebServlet("/RequestDemo2")
public class RequestDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("include request demo 2 ");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}