response重定向
package cn.edu.Response; 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; //实现请求重定向 /* 重定向的特点: 1.浏览器会向服务器发送两次请求,意味着就有两个request和response 2.用重定向技术,浏览器地址栏会发生变化 用户登录和显示购物车时,通常会用到重定向技术 */ public class ResponseDemo7 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("/day06/index.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
request实现转发
package cn.edu.Request; 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; //请求转发,以及使用request域对象把数据带给转发资源 //请求转发的特点: //1.客户端只发出一次请求,服务器端有多个资源调用 //2.浏览器地址栏没有变化 public class RequestDemo5 extends HttpServlet { //MVC(model javabean V View(jsp) C Controller(servlet)) public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data="aaaaa"; request.setAttribute("data",data); //request实现转发 request.getRequestDispatcher("/message.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
转发:你找我借钱,我没有,我帮你找他
重定向:你找我借钱,我没有,我让你自己去找他