Request forwarding: a way to redirect resources inside the server
Features:
- Forwarding is a request
- The path in the browser address bar will not change
- Can only be forwarded to the internal resources of the current server
Code demo:
// 获取请求转发器对象:RequestDispatcher RequestDispatcher rd = request.getRequestDispatcher(String path) //使用转发器调用forward();方法 rd.forward(ServletRequest request, ServletResponse response)
Request redirection: a way of resource redirection
Features:
- Redirect is two requests
- The path in the browser address bar will change
- Redirect to access resources of other sites (servers)
Code demo:
第一种写法 //1. 设置状态码为302 response.setStatus(302); //2.设置响应头location response.setHeader("location","/xxx/xx/x"); 第二种写法 response.sendRedirect("/xxx/xx/x");