Cause Analysis and Solution of jsp response. sendRedirect not redirection

Source: Internet
Author: User

I encountered a problem when I was working on the project recently. I added response. sendRedirect () and the system also executed it, but it just didn't jump. Finally, I found the reasons on the Internet:

First, we need to understand how to use response. sendRedirect is the principle of redirection. It actually sends a special Header to the browser and then redirects it to the specified page. Therefore, when sendRedirect is used, you can see the address changes in the address bar of the browser.
The difference between using <jsp: forward page = ""/> is that it is directly done on the server, and the browser does not know or deal with the browser, the address of the browser does not change.

Therefore, when using response. sendRedirect, pay attention to the following two points:

1. When response. sendRedirect is used, no HTML output is available.

This is not absolute. The absence of HTML output means that HTML cannot be sent to a browser. In fact, the current server has a cache mechanism, generally at 8 K (I mean JSP SERVER), which means that unless you disable the cache or you use out. flush () force refresh. A small amount of HTML output is allowed before sendRedirect is used.
If an error is reported, "Some information has been submitted" (the original text has been forgotten), you should check whether there are too many HTML outputs.

2. After response. sendRedirect, follow the return statement;

We already know that response. sendRedirect is switched through a browser, so there will be actual actions only after page processing is complete. Now that you have to turn, what is the significance of the subsequent output? In addition, the redirection may fail due to the subsequent output.

<% @ Include file = "/page/checkLogin. jsp" %> the first sentence on the page to be verified.

The content of checkLogin. jsp is determined by checking whether the user has logged on to the session. If not, it is redirected to the logon page:
Copy codeThe Code is as follows:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
<%
If (session. getAttribute ("userinfo") = null ){

Response. sendRedirect (url );

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.