资料来源于:B站尚硅谷JavaWeb教程(全新技术栈,全程实战) ,本人才疏学浅,记录笔记以供日后回顾
视频链接
知识点
1) 服务器内部转发 : request.getRequestDispatcher("...").forward(request,response);
- 一次请求响应的过程,对于客户端而言,内部经过了多少次转发,客户端是不知道的
- 地址栏没有变化
2) 客户端重定向: response.sendRedirect("....");
- 两次请求响应的过程。客户端肯定知道请求URL有变化
- 地址栏有变化
服务器内部转发示例图
客户端重定向示例图
代码示例
java代码(是2个类的,我就不分开写啦)
public class Demo06Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo06。。。");
//服务器端内部转发
// request.getRequestDispatcher("demo07").forward(request,response);
//客户端重定向
response.sendRedirect("demo07");
}
}
public class Demo07Servlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo07。。。");
}
}
web.xml代码
<servlet>
<servlet-name>Demo06</servlet-name>
<servlet-class>com.ypf.servlets.Demo06Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Demo06</servlet-name>
<url-pattern>/demo06</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Demo07</servlet-name>
<servlet-class>com.ypf.servlets.Demo07Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Demo07</servlet-name>
<url-pattern>/demo07</url-pattern>
</servlet-mapping>
标签:服务器端,重定向,request,demo07,转发,Servlet,response,客户端
From: https://www.cnblogs.com/fgcs111/p/17171450.html