In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve redirection technology in Servlet and JSP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1.RequestDispatcher.forward ()
It works on the server side. When using forward (), Servlet engine transmits the HTTP request from the current Servlet and 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 () cannot redirect to the jsp file with frame, it can be redirected to the html file with frame, and forward () cannot be passed with parameters, such as servlet?name=frank, so 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 and JSP
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, you cannot have 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) response.sendRedirect 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) .Request Dispatcher.forward () is the redirection of control in the container, and the redirected address is not 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 when the former can meet the needs, try to use the RequestDispatcher.forward () method.
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.
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 jsp:forward 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:
5.JSP implements automatic redirection to another page after staying on one page for a few seconds.
In the html file, the following code:
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.
From the review of the redirection technology in Servlet and JSP, we can use setHeader to realize the automatic redirection to another page after staying for a few seconds.
This is the end of this article on "how to achieve redirection in Servlet and JSP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.