Common JSP Jump Summary

Source: Internet
Author: User

This project, with several different ways of JSP jump, in this summary of 5 common METHODS.

There are several common ways to jump:

(1) href hyperlink tag, belong to client jump

(2) complete with javascript, belong to client jump

(3) Submit form Complete jump, belong to the client jump

(4) Use response object, belong to client jump

(5) use RequestDispatcher class, belong to server jump

Below hit see

(1) href hyperlink tag

This is relatively simple, usually written in a tag, to complete the dynamic jump of the specified position is more convenient

Code: <a href= "new.jsp" > Jump </a>

(2) complete with JavaScript

(i) the ability to write the submission form in JavaScript code

Code:

[javascript]
  1. <scripttype="text/javascript" >
  2. Function Submit () {
  3. with (document.getelementbyid ("queryfunction")) {
  4. action="new.jsp";
  5. method="post";
  6. Submit ();
  7. }
  8. }
  9. </script >

(ii) can also be directly positioned to assign values to the Window.location attribute without submitting the form

Code:

[javascript]View PlainCopy
    1. <script type="text/javascript" >
    2. function Go {
    3. Window.self.location = "new.jsp";
    4. }
    5. </script >

It can also be written in

[javascript]
    1. <script type="text/javascript" >
    2. Window.location.replace ("http://www.baidu.com");
    3. </script>

Replace this method and the positioning window.location difference is that he has no history record.

(iii) using the history Object's forward (), back (), go () method

Where the Go () method requires a shaping entry parameter

Code: <a href= "javascript:history.go ( -1)" > Back to previous </a> equivalent to <a href= "javascript:history.back ()" > Back to Previous step </a>

(3) Submit Form Complete jump

The way the entire form form data is Submitted.

Code:

[html]
  1. <form name= "form1"method= "POST" action="new.jsp">
  2. <input type="text" name="name">
  3. <input type="text" name= "psd">
  4. <input type="submit" value= "submission">
  5. <input type="reset" value= "reset" >
  6. </from>



(4) using the response object, the built-in object for the JSP

(i) Direct use of the Sendredirect () redirect, the URL of the redirected page appears on the Browser's address bar after Redirection. code:

<%

Response.sendredirect ("http://www.baidu.com");

Return

%>

Note: URLs in Sendredirect () can be parameterized, such as Sendredirect ("url?name=" +name), and we can pass in parameters when Jumping.

In addition, the general Response.sendredirect () followed by a return, we already know that Response.sendredirect is a browser to do the steering, so only after the completion of the page processing, there will be actual Action. Now that the steering has been done, then the output is meaningless, and it is possible that the subsequent output will cause the steering to Fail.

(ii) Use the SetHeader () method to directly modify the Address bar for page redirection

<%

Response.setheader ("Refresh", "1;url=http://www.baidu.com");

%>

Standard format: Response.setheader ("Refresh", "number of seconds to wait, url= absolute path or relative path"); the previous example is to wait 1 seconds before jumping.

(5) using the RequestDispatcher class

Basic methods

[java]
    1. RequestDispatcher rd = Request.getrequestdispatcher ("target page");
    2. Rd.forward (response,request);

A servlet can get RequestDispatcher objects in two ways:

Servletcontext's Getrequestdispatcher ()

Servletrequest's Getrequestdispatcher ()

Call Servletcontext's getrequestdispatcher (String Path) method, which specifies the path to the target Component.

The Getrequestdispatcher String path method that calls ServletRequest differs from the previous way in that the path parameter of the former must be an absolute path, while the Latter's path parameter can be either an absolute or a relative path. The so-called absolute path refers to the path that begins with the symbol "/" and "/" represents the URL entry for the current web app.

finally, Compare the difference between response redirect and forward jump

Response:1 executes all the code and jumps to the target page

------------------2 the URL of the browser changes after jumping to the target page

------------------3 redirect in browser

------------------4 can jump to a page on another server, such as "baidu".

Forward Jump: 1 Jump directly to the target page the code behind it no longer executes

------------------2 URL does not change after jumping to the target page

------------------3 directed at server end multiplicity

------------------4 cannot jump to a page on another server

Common JSP Jump Summary

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.