Five ways to jump a JSP page

Source: Internet
Author: User

Five ways to jump a JSP page

1. Requestdispatcher.forward ()

is on the server side, when using forward (), the servlet engine passes an HTTP request from the current Servlet or JSP to another servlet,jsp or plain HTML file, i.e. your form is submitted to a.jsp, The a.jsp used forward () redirect to B.jsp, at which time all the information submitted by the form is available in b.jsp, and the parameters are passed automatically. However, forward () cannot redirect to a frame JSP file, can be redirected to a frame HTML file, while forward () cannot be passed in the back with parameters, such as Servlet?name=frank, so no, 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 a 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 pages
Rd.forward (request, response);
}
Typically used in servlets and not used in JSPs.

2. Response.sendredirect ()

is to work in the user's browser, sendredirect () can be passed with parameters, such as Servlet?name=frank to the next page, while it can be redirected to a different host, Sendredirect () can redirect the JSP file with frame.

The URL of the redirect page appears on the browser address bar after redirection

Example: Redirecting in a servlet

public void DoPost (httpservletrequest request,httpservletresponse response)
Throws Servletexception,ioexception
{
Response.setcontenttype ("text/html; charset=gb2312 ");
Response.sendredirect ("/index.jsp");
}
Since response is a hidden object in a JSP page, it can be repositioned directly in the JSP page using Response.sendredirect ().

Attention:

(1) When using Response.sendredirect, the front cannot have HTML output;

This is not absolute, no HTML output actually means that no HTML can 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 Out.flush () force refresh, before using Sendredirect, A small amount of HTML output is also allowed.

(2) After response.sendredirect, it should be followed by a return.

We already know that Response.sendredirect is turned on by the browser, so only after the page processing is done will there be actual action. Since you have to do a turn, then what is the meaning of the output? It is also possible that the subsequent output causes a steering failure.

Comparison:

(1) Dispatcher.forward () is the control in the container of the steering, in the client browser address bar will not show the post-turn address;

(2) Response.sendredirect () is a full jump, the browser will get the address of the jump, and resend the request link. This way, you can see the link address after the jump from the address bar of the browser.

The former is more efficient and uses the Requestdispatcher.forward () method as much as possible when the former can meet the needs.

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.

The underlying part of it is implemented by RequestDispatcher, so it bears the imprint of the Requestdispatcher.forward () method.

If you have a lot of output before, and the previous output has the buffer full and will automatically output to the client, the statement will not work, which should be particularly important.

Also note: It can not change the browser address, refresh will lead to repeated submissions

4. Modify the Location property of the HTTP header to redirect

The redirection of the page is achieved by setting the address bar to modify 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 in the implementation of a page after a few seconds, automatically redirect to another page

In the HTML file, the following code:


What it means: the page that is being browsed after 5 minutes will automatically become the target.html page. The code in 300 is the refresh delay time, in seconds. Targer.html you want to turn to the target page, if this page is automatically refresh this page.

From the above, you can use SetHeader to implement a page for several seconds, automatically redirect to another page.

Key code:
String content=staytime+ "; Url= "+url;
Response.setheader ("REFRESH", content);

Copyright notice: I feel like I'm doing a good job. I hope you can move your mouse and keyboard for me to order a praise or give me a comment, under the Grateful!_____________________________________________________ __ Welcome reprint, in the hope that you reprint at the same time, add the original address, thank you with

Five ways to jump a JSP page

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.