Several methods of page jump under JSP _jsp programming

Source: Internet
Author: User

1. Requestdispatcher.forward ()
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 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 a frame, while forward () cannot be passed back with parameters such as Servlet?name=frank, which is not possible. You can pass Response.setattribute ("name", name) to the next page within your program.
  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{
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 a servlet and not used in a JSP.


2. Response.sendredirect ()
Works on the user's browser side, 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 redirect the JSP file with frame. The URL of the redirected page appears on the browser's address bar after the
redirection.
Example: Redirecting in servlet
public void DoPost (httpservletrequest request,httpservletresponse response)
  & nbsp;                             throws servletexception,ioexception{
Response.setcontenttype ("text/html; charset=gb2312 ");
Response.sendredirect ("/index.jsp");
}
because response is an implied object of a JSP page, you can use Response.sendredirect () to directly implement relocation in a JSP page.
Note:
(1) cannot have HTML output before it uses response.sendredirect;
This is not absolute, 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, it should be followed by a return.
We already know that Response.sendredirect is a browser to do the steering, so only when the page processing is complete, there will be actual action. Now that you've got to turn, what's the point of the output? And it is possible that the subsequent output will cause the steering failure.
comparison:
(1) Dispatcher.forward () is a diversion of control in the container and does not show a turned address in the client browser's address bar;
(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 and uses the Requestdispatcher.forward () method as much as possible when the former can meet the need.
In some cases, such as the need to jump to a resource on a different server, you must make the Httpservletresponse.sendrequest () method .


3.
Its underlying part is implemented by RequestDispatcher, so it carries the imprint of the Requestdispatcher.forward () method.
Special attention should be paid to the fact that if there were many outputs before, and the previous output had filled the buffer and would be automatically exported to the client, the statement would not work.
  Note: It cannot change the browser address, and refreshing will result in duplicate submissions


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:
<%
Response.setstatus (httpservletresponse.sc_moved_permanently);
String NEWLOCN = "/newpath/jsa.jsp";
Response.setheader ("Location", NEWLOCN);
%>


5. JSP to automatically redirect to another page after a page stops for several seconds
  In the HTML file, the following code:

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. The
is known by SetHeader to automatically redirect to another page after a page has been paused for several seconds. Code:
String content=staytime+; Url= "+url;
Response.setheader ("REFRESH", content);

Below is another user's article
1 request.getrequestdispatcher ("/admin.jsp"). Forward (request, response);//forward to admin.jsp

2. RequestDispatcher rd=request.getrequestdispatcher ("/admin.jsp");
Rd.forward (request, response);

3. Getservletconfig (). Getservletcontext (). Getrequestdispatcher ("/admin.jsp"). Forward (request, response);

Other methods of course can be used in HTML JS, the method requires JSP output, depending on the need to choose.

Related Article

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.