Jsp automatic jump Methods: jsp automatic jump

Source: Internet
Author: User

Jsp automatic jump Methods: jsp automatic jump

Requirements and prerequisites:

 

1. The current page is the first page of the project (welcome. jsp)

 

2. Access the project. After Entering welcome. jsp, the page automatically jumps to the index page through the springMVC request.

 

3. Directly accessing localhost: 8080/common/index can directly access the index page.

 

 

 

I. js redirection

 

1. onload + location. href or location. replace

 

Key code:

 

...

 

<%

 

String path = request. getContextPath ();

 

String basepath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

 

%>

 

...

 

Function commit (){

 

Location. href = "https://blog.csdn.net/yunduanyou/article/details/ <% = basepath %> common/index ";

 

}

 

...

 

 

 

Summary:

 

1. Add in head online

 

2. basepath is useful in jsp

 

3. Do not forget to add brackets when calling js functions. onload = "commit" won't work. The key is that there is no error message.

 

 

 

2. onload + form. submit ()

 

Key code:

 

...

 

Function commit (){

 

Var form = document. getElementById ("indexform ");

 

Form. action = "https://blog.csdn.net/yunduanyou/article/details/ <% = basepath %> common/index ";

 

Form. submit ();

 

}

 

...

 

 

 

 

...

 

Summary:

 

In my opinion, someone writes and submits a form like this on the Internet:

 

With (document. getElementById ("queryFunction ")){

 

Action = "new. jsp ";

 

Method = "post ";

 

Submit ();

 

}

 

With is used to set the scope of code in a specific object.

 

Although it looks a lot better, with is a code block that runs slowly and should be avoided as much as possible.

 

 

 

3. Tag a + js event triggering

 

Key code:

 

 

 

<Script language = "javascript">

 

Var comment = document. getElementsByTagName ('A') [0];

 

If (document. all ){

 

// For IE

 

Comment. click ();

 

} Else if (document. createEvent ){

 

// FOR DOM2

 

Var ev = document. createEvent ('mouseevents ');

 

Ev. initEvent ('click', false, true );

 

Comment. dispatchEvent (ev );

 

}

 

</Script>

 

Summary:

 

This code can be used to judge the browser.

 

It is also a way of thinking. I tried it and it is also easy to use.

 

 

 

Ii. jsp Mode

 

1. jsp: forward

 

Key code:

 

 

Summary:

 

On the Internet, the underlying part is implemented by RequestDispatcher, so it carries the marks of the RequestDispatcher. forward () method (I am not going to investigate it, remember it first)

 

 

 

2. response. sendRedirect

 

Key code:

 

Response. sendRedirect (basepath + "common/index ");

 

Summary:

 

Response. sendRedirect is a "client jump" method, which always corresponds

 

RequestDispatcher. forward (server jump, or "forwarding ". This is written in jsp, and an error will be reported during execution)

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.