Memo: JSP redirection. Use Out.print (); Response.sendredirect (); __js

Source: Internet
Author: User
Tags button type flush

Button type:

<input name= "Pclog" type= "button" value= "Go" onclick= "location.href= ' http://9ba.cn/'" ">

LINK Type:

<a href= "Javascript:history.go ( -1)" > Back to the previous step </a>

<a href= "<%=request.servervariables (" Http_referer ")%>" > Back to Previous step </a>

Direct Jump Type:

<script>window.location.href= ' http://www.9ba.cn ';</script>

Open a new window:

<a href= "javascript: onclick=" window.open (' http://www.9ba.cn/post/235.html ', ', ', ' height=500,width = 611, Scrollbars=yes,status=yes ') > The days on the cloud </a>



JSP Jump Way

There are approximately three ways to jump using JSPs:
1. Response.sendredirect ();
2. Response.setheader ("Location", "");
3. <jsp:forward page= ""/>

Some of the following rules have been tested:

I. Response.sendredirect ()

Out.flush () is not allowed before this statement, and if so, there will be an exception:
Java.lang.IllegalStateException:Can ' t Sendredirect () after data has committed to the client.
At Com.caucho.server.connection.AbstractHttpResponse.sendRedirect (abstracthttpresponse.java:558)
...
Browser address bar changes after jump
If you want to jump to a different host, after the jump, the statement after this statement will continue to execute, as the new thread, but the operation of the response has no meaning;
If you want to jump to the same host, the statement after this statement completes execution before jumping;
Two. Response.setheader ("Location", "")

Out.flush () is not allowed before this statement, and if so, the page will not jump.
Browser address bar changes after jump
The statement after this statement finishes execution does not jump
Three. <jsp:forward page= ""/>

Out.flush () is not allowed before this statement, and if so, there will be an exception:
Java.lang.IllegalStateException:forward () allowed after buffer has committed.
At Com.caucho.server.webapp.RequestDispatcherImpl.forward (requestdispatcherimpl.java:134)
At Com.caucho.server.webapp.RequestDispatcherImpl.forward (requestdispatcherimpl.java:101)
At Com.caucho.jsp.PageContextImpl.forward (pagecontextimpl.java:836)
...
The browser address bar does not change after the jump, but can only jump to the current host
The statement after this statement finishes execution does not jump

You cannot have any HTML output before Response.sendredirect ().

To output some hints before redirecting, you might think:

Out.println ("<script>alert (' error message ') </script>");
Response.sendredirect (index.html);
Return

But this simple idea, how also can not achieve.

No way.... I had to think otherwise.

1. Full output JavaScript with OUT.PRINTLN, complete redirection by JavaScript


Out.println ("<script>alert (' error message ') </script>");
Out.println ("<script>window.location.href= ' index.jsp");
Return

2. Flush to redirect page with header header


Out.println ("<script>alert (' error message ') </script>");
Response.setheader ("Refresh", "1;url=index.jsp");
Return

3. Use the Java Swing component's Joptionpane instead of the JavaScript cue box to complete the redirection by Sendredirect ()


Javax.swing.JOptionPane.showMessageDialog (NULL, error message);
Response.sendredirect ("index.jsp");
return;

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.