The review of Jsp/servlet technology is as follows
1. Requestdispatcher.forward ()
is on the server side, when using forward (), the Servlet engine passes HTTP requests from the current servlet or JSP to another servlet,jsp or normal HTML file, or your form commits to a.jsp, The forward () redirect to b.jsp is used in a.jsp, where all the information submitted by form is available in b.jsp and the parameters are automatically passed.
However, forward () cannot be redirected to a JSP file with a frame, which can be redirected to an HTML file with frame, while forward () cannot be passed back with parameters, such as a servlet? Name = Frank, this is not possible and can be passed to the next page within the program via Response.setattribute ("name", name).
The browser address bar URL does not change after redirection.
Example: Redirecting in the servlet
public void DoPost (httpservletrequest request,httpservletresponse response)
Throws Servletexception,ioexception
... {
}
Typically used in a servlet and not used in a JSP.
2. Response.sendredirect ()
is working on the user's browser side, Sendredirect () can be passed with parameters, such as servlet? Name = Frank is passed to the next page, and it can be redirected to a different host, Sendredirect () can redirect the JSP file with frame.
The URL of the redirected page appears on the browser's address bar after redirection
Example: Redirecting in the servlet
public void DoPost (httpservletrequest request,httpservletresponse response)
}
Because response is an implied object in JSP pages, you can use Response.sendredirect () directly in JSP pages to achieve relocation.
Attention:
(1). You cannot have HTML output before using Response.sendredirect.
This is not absolute, and the inability to have HTML output actually means that HTML cannot be sent to the browser. In fact, the server now has the cache mechanism, generally in 8K (I mean JSP SERVER), which means that unless you close the cache, or you use the Out.flush () force refresh, then before using Sendredirect, A small amount of HTML output is also allowed.
(2). After Response.sendredirect, should be followed by a return;
We already know that Response.sendredirect is a browser to do the steering, so only after the page processing is complete, there will be actual action. Now that you've got to turn, then what's the point of the output? And it is possible that the subsequent output will cause the steering failure.
Comparison:
(1). Request Dispatcher.forward () is the control in the container of the steering, in the client browser address bar will not show the direction of the address;
(2). Response.sendredirect () is a complete jump, the browser will get the address of the jump, and resend the request link. In this way, you can see the link address after the jump from the address bar of the browser.
The former is more efficient, when the former can meet the needs, try to use the Requestdispatcher.forward () method.
Note: In some cases, for example, you need to jump to a resource on a different server, you must use the Httpservletresponse.sendrequest () method.
3. < Jsp:forward page = ""/>
Its underlying part is implemented by RequestDispatcher, so it carries the imprint of the Requestdispatcher.forward () method.
If you have a lot of output before < Jsp:forward >, the previous output is full and will be automatically output to the client, then the statement will not work, and this should be particularly noted.
Also note: It can not change the browser address, the refresh will result in duplicate submission
4. Modify the HTTP header's Location property to redirect
You can redirect the page by setting the address bar directly.
The JSP file code is as follows:
5. The implementation of the JSP in a page after a few seconds, automatically redirected to another page
In the HTML file, the following code:
< meta HTTP-EQUIV = "refresh" content = "300; Url=target.jsp ">
What it means: the page that you are browsing after 5 minutes will automatically change to target.html page. The delay time, in seconds, for the refresh of 300 in the code. Targer.html for the target page you want to turn to, if this page is the automatic refresh this page.
From the above, you can use SetHeader to achieve a page after a few seconds, automatically redirect to another page.
Key code:
String content = Staytime + "; Url= "+ URL;
Response.setheader ("REFRESH", content);
If the summary is not comprehensive enough, please give your opinion or experience.
Note: Part of the data from: http://blog.csdn.net/doli/archive/2002/11/28/13203.aspx
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.