request对象请求转发小例子笔记
2020-02-17 05:25
176 查看
请求转发:一种在服务器内部资源跳转的方式。
步骤
1.通过requeest对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)。
2.使用这个对象来进行转发:forward(ServletRequest request , ServletResponse response)。
小例子:
@WebServlet("/RequestDemo8") public class RequestDemo8 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo8被访问了。。"); //转发到demo9资源 /*RequestDispatcher requestDispatcher = request.getRequestDispatcher("/RequestDemo9"); requestDispatcher.forward(request,response);*/ //推荐使用的方法 request.getRequestDispatcher("/RequestDemo9").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
@WebServlet("/RequestDemo9") public class RequestDemo9 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo9被访问了。。"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
控制台输出
特点:
1.浏览器地址栏路径不发生变化。
2.只能转发到当前服务器内部资源中。
3.即使转发多个资源路径,也只向服务器请求一次,是最开始的那次。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- SAP CRM WebClient UI和Hybris Commerce的懒加载机制
- SAP UI5 Web Component的图标实现
- CRM WebClient UI里Sales area的保存原理
- 使用SAP Cloud Platform fullstack WebIDE创建SAP UI5应用并部署
- Filter对Request的改变:HttpServletRequestWrapper的工作原理
- ServletRequestListener的用法和实例
- jsp inclue servlet。jsp包含servlet
- UGUI Text添加滚动条 实现滚动显示
- UI 2D图片随鼠标旋转
- Mysql Field * doesn't have a default value解决方法
- 1062 Talent and Virtue (25分)
- Building XML File in C#
- Project ERROR: Unknown module(s) in QT: qml quick
- poj3368 frequent values (快状数组)
- 关于MOSQUETTO配置文件mosquetto.conf的配置
- Grails Quick Start
- iOS开发 粗解UIDynamicAnimator
- Fluent NHibernate的配置
- C# 浅谈ThreadPool -- 上篇(Enqueue)
- @RequestMa'pping注解 value的值