当前位置:网站首页>The Servlet to jump to the JSP page, forwarding and redirection
The Servlet to jump to the JSP page, forwarding and redirection
2022-08-05 06:13:00 【CrazyQiQi】
The method of servlet jumping page:
The jump is divided into two parts, one part happens in servlet, the other part happens in JSP, JSP is servlet, but there are still some differences between the two.
Servlet: (Jump occurs in doGet, doPost and other methods)
(1) Redirect method
response.sendRedirect(“/index.jsp”); the path of the pageis a relative path.sendRedirect can jump the page to any page, not necessarily limited to web applications, such as: response.sendRedirect ("http://www.baidu.com");
After the jump, the browser address changes, onlyYou can always bring parameters in the url or put them in the session, you cannot use request.setAttribute to pass values.
(2) Forward method
request.getRequestDispatcher(“/index.jsp”).forward(request,response); The path of the page is the right path.The forward method can only jump to the page in this web application.The address bar does not change after jumping.Using this method to jump, you can use three methods to pass the value: parameter, session, request.setAttribute in the url
JSP:
(1) response.sendRedirect();
The same as the response.sendRedirect() of servlet
After the jump, the browser address bar will change,If you want to jump to a different host, after the jump, the statement after this statement is like a new thread, but the operation of the response is meaningless; if you want to jump to the same host, the statement after this statement is executed.After the jump, it will jump;
(2) response.setHeader(“Location”,””);
After the jump, the browser address bar will change, and the transfer will not occur until the statement following this statement is executed.
(3) After the jump, the browser address bar remains unchanged, but it can only jump to the current host, and the execution statement after this statement will not jump until the execution is completed
A summary comparison of the RequestDispatcher.forward method for request forwarding and the HttpServletResponse.sendRedirect method for response (redirection) forwarding:
1. The RequestDispatcher.forward method can only forward requests to the same web applicationcomponents in the HttpServletResponse.sendRedirect method can redirect not only to other resources in the current application, but also to resources in other applications on the same site, or even redirect to other sites using absolute URLsresource.If the relative URL passed to the HttpServletResponse.sendRedirect method begins with "/", it is relative to the root directory of the entire web site; if the relative URL specified when creating the RequestDispatcher object begins with "/", it is relative to the current web application's root directory.
2. After the redirected access process by calling the HttpServletResponse.sendRedirect method is over, the URL displayed in the browser address bar will change, and the initial URL address will be redirected to the target URL; and the request that calls the RequestDispatcher.forward method will be forwardedAfter the process is over, the browser address bar keeps the original URL address unchanged.
Three, the HttpServletResponse.sendRedirect method directly responds to the browser request, and the result of the response is to tell the browser to re-send the access request to another URL; the RequestDispatcher.forward method has forwarding behavior inside the server.
4. The caller and the callee of the RequestDispatcher.forward method share the same request object, and they belong to the same access request and response process; and the caller and the callee of the HttpServletResponse.sendRedirect method use their own request objectsand response objects, which belong to two separate access request and response processes.For the jump between the internal resources of the same web application, especially before the jump, some pre-processing of the request should be performed, and the HttpServletRequest.setAttribute method is used to pass the pre-processing result, then the RequestDispatcher.forward method should be used.Redirection between different web applications, especially to redirect to a resource on another web site, should use the HttpServletResponse.sendRedirect method.
Five, no matter which method, before calling them, no content has been actually output to the client.If the buffer already has some contents, those contents will be cleared from the buffer.
There is an important difference between redirection and request forwarding: when request forwarding is used, the JSP container will use an internal method to call the target page, and the new page continues to process the same request, whileThe browser will not know about this process.In contrast, the meaning of redirection is that the first page notifies the browser to send a new page request.Because, when you use redirection, the URL displayed in the browser becomes the URL of the new page, and when you use forwarding, the URL remains the same.Redirects are slower than forwards because the browser has to make a new request.At the same time, because the redirection method generates a new request, after a redirection, the object in the request will not be available.
—————————————————
Original link
边栏推荐
- The idea of commonly used shortcut key
- spark源码-任务提交流程之-4-container中启动executor
- 账号与权限管理
- VRRP原理及命令
- spark operator-parallelize operator
- 【Machine Learning】1 Univariate Linear Regression
- Unity物理引擎中的碰撞、角色控制器、Cloth组件(布料)、关节 Joint
- Introductory document 05-2 use return instructions the current task has been completed
- TCP/IP four-layer model
- ROS视频教程
猜你喜欢

The problem of calling ds18b20 through a single bus

NIO工作方式浅析

运维工程师,快来薅羊毛

【Day8】使用LVM扩容所涉及的命令

Configuration of TensorFlow ObjecDetectionAPI under Anaconda3 of win10 system

Hard Disk Partitioning and Permanent Mounting

Autoware--北科天绘rfans激光雷达使用相机&激光雷达联合标定文件验证点云图像融合效果

VRRP principle and command

硬盘分区和永久挂载

I/O性能与可靠性
随机推荐
D39_向量
unity实现第一人称漫游(保姆级教程)
[Paper Intensive Reading] The relationship between Precision-Recall and ROC curves
入门文档01 series按顺序执行
spark源码-任务提交流程之-7-流程梳理总结
markdown编辑器模板
解决这三大问题,运维效率将超90%的医院
spark算子-wholeTextFiles算子
Getting Started 03 Distinguish between development and production environments ("hot update" is performed only in the production environment)
D41_缓冲池
【Day6】文件系统权限管理 文件特殊权限 隐藏属性
【Day1】VMware软件安装
Wireshark抓包及常用过滤方法
OpenCV3.0 兼容VS2010与VS2013的问题
腾讯云云函数SCF—入门须知
什么?CDN缓存加速只适用于加速静态内容?
One-arm routing and 30% switch
Spark source code - task submission process - 4-container to start executor
js动态获取屏幕宽高度
dsf5.0新建页面访问时重定向到首页的问题