JSP page Jump method summary, jsp jump Summary

Source: Internet
Author: User

JSP page Jump method summary, jsp jump Summary

The following methods can be used to redirect JSP pages:

N use the href hyperlink to mark (client jump)
N use JavaScript (client jump)
N submit form (client jump)
N use response object (client jump)
N use the forward action flag (server jump)
N use the RequestDispatcher class (server jump)

1. Use response object

(1) Call the sendRedirect () method to redirect.

Note the following:

Ø sendRedirect () can be passed with Parameters

For example:

sendRedirect(“/main.jsp?userName=zhangsan”
 

Ø after sendRedirect (), follow the return statement.

SendRedirect () is switched through the browser, so there will be actual actions only after page processing is complete. Now that you have to turn, what is the significance of the subsequent output? In addition, the redirection may fail due to the subsequent output.

(2) Call the setHeader () method. wait a few seconds and redirect to another page.

You can call the setHeader ("Refresh", "Time; Target page") method through the response object to automatically redirect to another page after several seconds.

response.setHeader("Refresh", "10; url=login.jsp");  


The code above indicates that after 10 seconds, the login. jsp page will be automatically redirected.

2. Use the forward action flag

Difference between response redirection and forward redirection

(1) response redirection

N. After executing all the code on the page, jump to the target page.
N after you jump to the target page, the URL in the browser's address bar changes.
N redirection on the browser side.
N can jump to pages on other servers,

For example:response.sendRedirect(“http://www.baidu.com”)

(2) forward jump

N directly jumps to the target page, and the subsequent code is not executed.
N after you jump to the target page, the URL in the browser's address bar will not change.
N redirection on the server side.
N cannot jump to pages on other servers.

3. Use the RequestDispatcher class

Basic usage:

RequestDispatcher rd = request. getRequestDispatcher ("target page"); rd. forward (request, response );

The basic principle of using RequestDispatcher to redirect a page is the same as that of using the forward action tag.

Difference between response redirection and RequestDispatcher redirection

(1) response redirection

N. After executing all the code on the page, jump to the target page.
N after you jump to the target page, the URL in the browser's address bar changes.
N redirection on the browser side.
N can jump to pages on other servers,

Example: response. sendRedirect ("http://www.baidu.com ")

(2) Jump to RequestDispatcher

N. After all the code is executed, go to the target page.
N after you jump to the target page, the URL in the browser's address bar will not change.
N redirection on the server side.
N cannot jump to pages on other servers.

Difference between forward jump and RequestDispatcher jump

(1) forward jump

The code after the n forward action is marked is no longer executed. Jump to the target page immediately.
N when you specify the target page, you can use either an absolute path or a relative path.

(2) Jump to RequestDispatcher

N after executing all the code, including all the code after RequestDispatcher, go to the target page.
N when the target page is specified, only absolute paths can be used.

The above is a summary of the JSP page Jump method introduced by the small editor. I hope it will be helpful to you. If you have any questions, please leave a message. The small editor will reply to you in time!

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.