JSP page jumps and refreshes the summary __js

Source: Internet
Author: User
Summary of JSP page jump1. Requestdispatcher.forward () acts on the server side, and when using forward (), the Servlet engine passes HTTP requests from the current servlet or JSP to another servlet, JSP or normal HTML file, that is, your form submitted to a.jsp, the a.jsp used forward () redirect to B.jsp, when the form submitted all the information in the b.jsp can be obtained, 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 ()
In the user's browser-side work, Sendredirect () can be passed with parameters, such as Servlet?name=frank to the next page, while it can be redirected to different hosts, Sendredirect () can redirect the JSP file with frame.
After redirection, the URL of the redirected page appears on the browser's address bar.
Example: Redirecting in the servlet
public void DoPost (httpservletrequest request,httpservletresponse response)
Throws servletexception,ioexception{
Response.setcontenttype ("text/html; charset=gb2312 ");
Response.sendredirect ("/index.jsp");
}
   because response is the implied object of JSP pages, it can be response.sendredirect () directly in the JSP page to achieve relocation.
Attention:
(1) When using Response.sendredirect, the front cannot have the HTML output;
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) The Dispatcher.forward () is the steering of the control in the container, which will not show the turning address in the client browser 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, when the former can meet the needs, try to use the Requestdispatcher.forward () method.
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 implementation in a page after a few seconds, automatically redirect to another page
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.
From the above, you can use SetHeader to achieve a page after a few seconds, automatically redirect to another page. Code:
String content=staytime+ "; Url= "+url;
Response.setheader ("REFRESH", content);
Summary of JSP page refresh
1)

<metahttp-equiv= "Refresh" content= "10;url= jump page" >

10 indicates a refresh interval of 10 seconds

2)

<scriptlanguage= ' JavaScript ' >

Window.location.reload (TRUE);

</script>

If you want to refresh an iframe, replace the window with the name or ID number of the frame.

3)

<scriptlanguage= ' JavaScript ' >

Window.navigate ("This page URL");

</script>

4)

Function ABC ()

{

Window.location.href= "/blog/window.location.href";

SetTimeout ("ABC ()", 10000);

Refresh this page:

Response.Write ("<scriptlanguage=javascript>window.location.href=window.location.href;</script>")

Refresh Parent page:

Response.Write ("<scriptlanguage=javascript>opener.location.href=opener.location.href;</script>")

Go to the specified page:

Response.Write ("<scriptlanguage=javascript>window.location.href= ' yourpage.aspx ';</script>") timed refresh:

1,<script>settimeout ("location.href= ' url '", Watts) </script>

Description: URL is the page URL address to refresh

2000 is the wait time = 2 seconds,

2,<meta name= "Refresh" content= "N;url" >

Description

N, is the time to wait, in seconds

URL is the URL address of the page to be refreshed

3,<%response.redirect url%>

Note: Generally use a URL parameter or table conveys value to determine whether an operation occurred, and then use Response.Redirect refresh.

4, Refresh frame page 〈scriptlanguage=javascript>top.leftfrm.location.reload ();p arent.frmTop.location.reload (); </script 〉



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.