Forward and redirect

Request forwarding: a way to redirect resources inside the server

Features:

  1. Forwarding is a request
  2. The path in the browser address bar will not change
  3. 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:

  1. Redirect is two requests
  2. The path in the browser address bar will change
  3. 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");

Guess you like

Origin blog.csdn.net/weixin_45864391/article/details/106342456