* 请求转发只能转到到内部资源
* 转发的行为是服务器内部发出
*/
request.getRequestDispatcher("../day10/index.html").forward(request,
response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
doGet(request, response);
}
}
2、请求重定向
public class RedirectDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
//往request对象保存数据
request.setAttribute("name", "eric");
//请求重定向
// response.sendRedirect("/day11/TargetDemo");
/**
* 请求重点向:可以重定向到内部资源和外部资源
* 重定向的行为是浏览器发出
*/
// response.sendRedirect("http://www.baidu.com");
response.sendRedirect("/day10/index.html");
}
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
doGet(request, response);
}
}
3、转发/重定向到的目标 servlet
public class TargetDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
//从request对象获取数据
String name = (String)request.getAttribute("name");
System.out.println("通过request传递过来的数据:"+name);
}
评论1
最新资源