In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what the JSP page jump method has, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.
1. RequestDispatcher.forward ()
It works on the server side. When using forward (), Servlet engine transmits the HTTP request from the current Servlet or JSP to another Servlet,JSP or ordinary HTML file, that is, your form is submitted to a.jsp, and forward () is redirected to b.jsp in a.jsp. At this time, all the information submitted by form can be obtained in b.jsp, and the parameters are passed automatically. But forward () can not be redirected to the jsp file with frame, can be redirected to the html file with frame, while forward () cannot be passed with parameters, such as servlet?name=frank, this is not good, it can be transferred to the next page through response.setAttribute ("name", name) in the program.
The browser address bar URL remains unchanged after redirection.
Example: redirect in servlet
Public void doPost (HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {response.setContentType ("text/html; charset=gb2312"); ServletContext sc = getServletContext (); RequestDispatcher rd = null; rd = sc.getRequestDispatcher ("/ index.jsp"); / / directed page rd.forward (request, response);}
It is commonly used in servlet, not in jsp.
2. Response.sendRedirect ()
It works on the user's browser, sendRedirect () can be passed with parameters, such as servlet?name=frank to the next page, and it can be redirected to different hosts, sendRedirect () can be redirected to have frame. Jsp file.
After redirection, the URL of the redirected page appears on the browser address bar.
Example: redirect in servlet
Public void doPost (HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {response.setContentType ("text/html; charset=gb2312"); response.sendRedirect ("/ index.jsp");}
Because response is an implicit object in a jsp page, you can use response.sendRedirect () to relocate directly in a jsp page.
Note:
(1) when using response.sendRedirect, there must be no HTML output in front of it.
This is not absolute, no HTML output actually means that no HTML can be sent to the browser. In fact, today's server has a cache mechanism, usually at 8K (I mean JSP SERVER), which means that unless you turn off cache, or you use out.flush () to force refresh, then a small amount of HTML output is allowed before using sendRedirect.
(2) after response.sendRedirect, it should be followed by a return.
We already know that response.sendRedirect redirects through the browser, so there is no actual action until the page is processed. Now that you're about to turn, what's the point of the later output? And it is possible that the steering will fail because of the later output.
Compare:
(1) Dispatcher.forward () is the transfer of control in the container, and the redirected address will not be displayed in the client browser address bar.
(2) response.sendRedirect () is a complete jump, and the browser will get the address of the jump and resend the request link. In this way, the jumped link address can be seen from the browser's address bar.
The former is more efficient, and the RequestDispatcher.forward () method is used as much as possible when the former meets the needs.
Note: in some cases, for example, if you need to jump to a resource on another server, you must use the HttpServletResponse.sendRequest () method.
3. < jsp:forward page= "/ >
Its underlying part is implemented by RequestDispatcher, so it bears the imprint of the RequestDispatcher.forward () method.
If there is a lot of output before, and the previous output has filled the buffer and will be automatically output to the client, then this statement will not work, which should be paid special attention.
Also note: it cannot change the browser address, if refreshed, it will lead to repeated submission
4. Modify the Location property of HTTP header to redirect
Redirect the page by setting the address bar to modify it directly.
The jsp file code is as follows:
<% response.setStatus (HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocn = "/ newpath/jsa.jsp"; response.setHeader ("Location", newLocn);% >
5. JSP implements automatic redirection to another page after staying on one page for a few seconds
In the html file, the following code:
< meta http-equiv= "refresh" content= "300; url=target.jsp" >
What it means: the page you are browsing after 5 minutes will automatically become the target.html page. 300 in the code is the delay time for refresh, in seconds. Targer.html is the target page you want to redirect. If it is this page, it will refresh this page automatically.
As can be seen from the above, you can use setHeader to automatically redirect to another page after staying for a few seconds.
Key code:
String content=stayTime+ "; URL=" + URL;response.setHeader ("REFRESH", content); Thank you for reading this article carefully. I hope the article "what are the methods of JSP page jump" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.