当前位置:网站首页>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
边栏推荐
- 7步完成云上监控
- OpenCV3.0 兼容VS2010与VS2013的问题
- 2020,Laya最新中高级面试灵魂32问,你都知道吗?
- 入门文档12 webserve + 热更新
- lvm logical volume and disk quota
- Getting Started Documentation 12 webserve + Hot Updates
- 入门文档09 独立的watch
- Getting Started Document 09 Standalone watch
- 入门文档08 条件插件
- I217-V network disconnection problem in large traffic under openwrt soft routing
猜你喜欢
随机推荐
正则表达式小实例--验证邮箱地址
[Day1] VMware software installation
D39_坐标转换
Account and Permission Management
spark算子-wholeTextFiles算子
Unity物理引擎中的碰撞、角色控制器、Cloth组件(布料)、关节 Joint
VLAN details and experiments
Apache配置反向代理
js动态获取屏幕宽高度
LeetCode面试题
D46_给刚体施加的力
什么?CDN缓存加速只适用于加速静态内容?
spark source code-RPC communication mechanism
【Day8】(超详细步骤)使用LVM扩容
【Day8】使用LVM扩容所涉及的命令
NIO工作方式浅析
7步完成云上监控
Remembering my first CCF-A conference paper | After six rejections, my paper is finally accepted, yay!
【Day8】磁盘及磁盘的分区有关知识
TensorFlow ObjecDetectionAPI在win10系统Anaconda3下的配置