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)