JSP page Jump Methods

Source: Internet
Author: User

1. response. sendredirdbms ("Jump to page ");

This method modifies the http header to issue redirection instructions to the browser so that the browser displays the content of the redirected webpage.

The request cannot pass the value.

Run all the code on the page and jump to the page.

Jump to the address bar to change.

You can jump to the page response. sendredirdbms (http://www.sun.com) on another server ).


2. response. setHeader ();

This method is the same as response. sendRedirect by modifying the http header.


<%

Response. setStatus (HttpServletResponse. SC _MOVED_PERMANENTLY );

String newLocn = "/index.html ";

Response. setHeader ("Location", newLocn );

%>


3. <jsp: forward page = "Jump page"/>

This method uses the server to first output data to the buffer. Before the buffer content is sent to the client, the original content on the page will not be sent, if there are a lot of output before <jsp: forward>, and the previous output will automatically output to the client when the buffer is full, this statement will not work. Pay special attention to this.

Request can pass the value in the past.

Directly jump to the page without executing the code.

The address bar remains unchanged after the jump.

The page cannot be redirected to another server.

The image cannot be displayed because it is not an absolute path.


Example 1:

From. jsp

<% @ Page contentType = "text/html; charset = gb2312" %>

<% @ Page buffer = "1kb" %>

<%

Long I = 0;

Int j = 0;

For (I = 0; I <60; I ++)

{

Out. println (j ++ );

}

%>

<Jsp: forward page = "to. jsp"/>


Result: Jump to. jsp.


Example 2

From. jsp

<% @ Page contentType = "text/html; charset = gb2312" %>

<% @ Page buffer = "1kb" %>


<%

Long I = 0;

Int j = 0;

For (I = 0; I <600; I ++)

{

Out. println (j ++ );

}

%>

<Jsp: forward page = "to. jsp"/>

 

Error:


Java. lang. IllegalStateException: Error: Attempt to clear a buffer that's already been flushed

Org. apache. jasper. runtime. PageContextImpl. doForward (PageContextImpl. java: 648)

Org. apache. jasper. runtime. PageContextImpl. forward (PageContextImpl. java: 637)

Org. apache. jsp. forward. from02_jsp. _ jspService (from02_jsp.java: 58)

Org. apache. jasper. runtime. HttpJspBase. service (HttpJspBase. java: 94)

Javax. servlet. http. HttpServlet. service (HttpServlet. java: 802)

Org. apache. jasper. servlet. JspServletWrapper. service (JspServletWrapper. java: 324)

Org. apache. jasper. servlet. JspServlet. serviceJspFile (JspServlet. java: 292)

Org. apache. jasper. servlet. JspServlet. service (JspServlet. java: 236)

Javax. servlet. http. HttpServlet. service (HttpServlet. java: 802)

Org. pcvit. pan. filter. EncodingFilter. doFilter (EncodingFilter. java: 19)

 

Example 3:

From. jsp

<% @ Page contentType = "text/html; charset = gb2312" %>

<% @ Page buffer = "1kb" %>


<%

Long I = 0;

Int j = 0;

For (I = 0; I <6000; I ++)

{

Out. println (j ++ );

}

%>

<Jsp: forward page = "to. jsp"/>

 

Result: The code execution result on the from. jsp page is displayed.


------------------------------------------------------------------------------


?


------------------------------------------------------------------------------

 

4. request. getRequestDispatcher ("Jump to page ");

Request can pass the value in the past.

Run all the code on the page and jump to the page.

The jump Address Bar remains unchanged.

Cannot jump to pages on other servers


<%

RequestDispatcher rd = request. getRequestDispatcher ("to. jsp ");

Rd. forward (request, response );

%>


------------------------------------------------------------------------------


PS:


Server output buffer

By default, the content that the server will output to the client is written to an output buffer instead of the client. the buffer content is output to the client only in the following three cases:

Output of the JSP webpage complete information

The output buffer is full.

JSP calls out. flush () or response. flushbuffer ()

The size of the output buffer can be set with: or response. setBufferSize (), as follows:

Set the size of the output buffer to 1 kb. Or response. setBufferSize (1 );

Set the size of the output buffer to 0, that is, no buffer. Or response. setBufferSize (0 );

Use response. getBufferSize () or out. getBufferSize () indicates the size of the output buffer, in the unit of Word Section. use response. isCommitted () can be checked to check whether the server has output data to the client. if the return value is true, the data has been output to the client. If the return value is false, no data is available.


**************************************** **************************************** ***********

There are three methods to achieve output redirection:

RESPONSE. SETREDERECT ("URL") This method modifies the http header to issue a redirection command to the browser so that the browser displays the content of the redirected webpage. response. sendRedirect ("http: // localhost: 7001/index.html ");
The following method can also change the http header attribute. Its principle is the same as that of 1.
<%
Response. setStatus (HttpServletResponse. SC _MOVED_PERMANENTLY );
String newLocn = "/index.html ";
Response. setHeader ("Location", newLocn );
%>
The <JSP: FORWORD> method is used to output data to the buffer zone first on the server side. The original method is not sent before the buffer content is sent to the client, if there are a lot of output before <JSP: FORWORD> and the previous output has filled the buffer and will be automatically output to the client, the statement will not work, pay special attention to this.
Note:
1. Methods (1), (2) can use variables to represent the redirected address, and methods (3) cannot use variables to represent the redirected address.
String add = "./index.html ";
<Jsp: forward page = add/>
Go to index.html without authorization

String add = http: // localhost: 7001/index.html
Response. sendRedirect (add );
You can redirect to http: // localhost: 7001/index.html.

2. use the method (1), (2) variables in the request (through the request. setAttribute () values saved to the request) cannot be used in the new page. Method (3) can be used. in summary, we should adopt (1) and (2) redirection better.

**************************************** **************************************** *******

Use Response Buffering

By opening "response buffering", you can buffer the whole page content worth outputting, which minimizes the amount of data output to the browser, thus improving the overall performance. Each output consumes a lot, so the less you write, the better the effect. When sending a small number of large data packets, TCP/IP is more efficient than sending a large number of small data packets because it starts slowly and keeps sending.

There are two ways to open Response Buffering. First, you can use Internet Services Manager to enable response buffering for the entire application. This is a recommended method. In IIS4.0 and IIS5.0, response buffering is enabled by default. Next, you can place the following code in the header on each page to open response buffering:

<% Response. Buffer = True %>

This code must be executed before any data is output to the browser (that is, before any html content is displayed and before any cookie is set ). In general, opening response buffering for the entire application is a good solution. after doing so, you do not need to set the above Code in the header of each page.

A common problem about opening response buffering is that you must wait until all the pages are generated before you can see the content. For a page that runs for a long time, you can set Response. Buffer = False to disable the Buffer. Then, the good policy is to use the Response. Flush method, which will output all HTML content described by ASP to the browser. For example, after describing the 1,000 rows of a 100-row table, ASP can use Response. flush to force the output of the 100 rows to the browser, then the user can see the first 100 rows of data, while the remaining rows of data are being generated.

Note: For example of table output in row 1,000 above, some Browsers Do not output any content in the table unless marked with </table>. In this case, you can split the table into multiple tables with a small number of rows, and call Response. Flush to output the output after each table is generated. The new version of Internet Explorer displays the content only after the entire table is downloaded. If the column width of the table is defined, the table generation speed is extremely fast.

Another question about opening response buffering is that when a very large page is generated, the server memory will be greatly consumed.


**************************************** *************

1, add this sentence between The value 10 after CONTENT indicates the number of seconds. The URL can be followed by a relative path file name or an absolute http path.
This method is a client jump.


2. JS automatic jump
<Table>
<Tr>
<Td id = "go_title"> automatically jumps to the Forum homepage 3 seconds later </td>
</Tr>
</Table>
Var time = 0;
Function gotoBbsIndex (){
Time ++;
If (time = 3 ){
Document. location. href = "bbs/main/index.html ";
}
Document. getElementById ("go_title"). innerHTML = (3-time) + "automatically jump to the Forum homepage in seconds ";
}
Window. setInterval ("gotoBbsIndex ()", 1000 );
</Script>

Author: "Ying Feng. Java"
 

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.