request的请求转发与response的重定向区别
request和response都能实现网页跳转,其各自code如下
请求转发代码实现:
```1.通过ServletContent.getReqDisPathcer()获取ReqDisPathcer对象,再调用该对象的forward()实现请求转发RequestDispatcher requestDispatcher= this.getServletContext().getRequestDispatcher("/project/demo");requestDispatcher.forward(request,response);2.通过Request.getDisPatcher()获取ReqDisPatcher对象,再调用该对象的forward()实现请求转发RequestDispatcher requestDispatcher=request.getRequestDispatcher("/project/demo").forward(request,response);```
重定向代码实现:
```1.//设置状态码为302response.setStatus(302);//设置响应头locationresponse.setHeader("location","/project/demo");2.//简单的重定向方法response.sendRedirect("/project/demo");```
重定向(redirect)的特点
1.地址栏发生变化
2.重定向可以访问其他站点(服务器)的的质押
3.重定向是两次请求,不能使用request对象共享数据转发(forward)特点
1.转发地址栏路径不变
2.转发只能访问当前服务器下的资源
3.转发是一次请求,可以使用request对象共享数据