使用Response实现重定向
常见场景:
- 用户登录
public void sendRedirect(String location) throws IOException;
使用Response的sendRedirect(请求路径),实现请求重定向。
转发与重定向的区别:
相同点:页面都会跳转;
不同点:
- 请求转发时,url不会发生变化;
- 重定向时,浏览器地址栏的url会发生变化。
public class Servlet4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 重定向的时候,一定要注意路径问题,否则就会出现404
resp.sendRedirect("/s1/index.jsp");
}
}