Summary of JSP Page Jump Method

  • 2021-11-13 08:38:25
  • OfStack

There are several ways to realize JSP page jump:

n Use href Hyperlink Tag (Client Jump)
n uses JavaScript (Client Jump)
n Submit Form (Client Jump)
n uses response objects (client jump)
n uses forward action tokens (server-side jump)
n uses the RequestDispatcher class (server-side jump)

1. Use the response object

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

Note the following two points:

& Oslash; sendRedirect () can be passed with parameters

For example:


sendRedirect( " /main.jsp?userName=zhangsan " 

& Oslash; return should be followed by sendRedirect ()

sendRedirect () is navigated through the browser, so there is no real action until the page is processed. Now that you have to make a turn, what's the significance of the following output? And it is possible that the steering will fail because of the subsequent output.

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

The setHeader ("Refresh", "Time; Target page") method can be called through the response object to automatically redirect to another page after waiting for several seconds.


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


The above code indicates that after waiting for 10 seconds, it will automatically redirect to the page login. jsp.

2. Use forward action markers

Differences between response Redirection and forward Jump

(1) response redirection

n executes all the code for the page and then jumps to the target page.
After n jumps to the target page, URL in the browser address bar changes.
n is redirected on the browser side.
n can jump to pages on other servers,

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

(2) forward jump

n jumps directly to the target page, and the subsequent code is no longer executed.
After n jumps to the target page, URL in the browser address bar does not change.
n is redirected on the server side.
n cannot jump to a page on another server.

3. Use the RequestDispatcher class

Basic usage:


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

Using RequestDispatcher to realize page jump, and using forward action tag to realize page jump, its basic principle is 1 sample.

The difference between response redirection and RequestDispatcher jump

(1) response redirection

n executes all the code for the page and then jumps to the target page.
After n jumps to the target page, URL in the browser address bar changes.
n is redirected on the browser side.
n can jump to pages on other servers,

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

(2) RequestDispatcher jump

n executes all the code and then jumps to the target page.
After n jumps to the target page, URL in the browser address bar does not change.
n is redirected on the server side.
n cannot jump to a page on another server.

The difference between forward jump and RequestDispatcher jump

(1) forward Jump

The code that follows the n forward action tag is no longer executed and immediately jumps to the target page.
n can use either absolute or relative paths when specifying a target page.

(2) RequestDispatcher Jump

n executes all code, including all code after RequestDispatcher, and then jumps to the target page.
n When specifying a target page, only absolute paths can be used.

The above is the site to introduce you JSP page jump method summary, I hope to help you, if you have questions welcome to leave me a message, this site will reply to you in time!


Related articles: