The reason analysis and solution of JSP Response.sendredirect not jump _jsp programming

Source: Internet
Author: User
Recently did the project encountered a problem, obviously added Response.sendredirect (), the system also implemented, but it is not jump; finally find the reason on the Internet as follows:

First of all, we need to understand the principle of response.sendredirect, it is actually to the browser to send a special header, and then the browser to do the turn, go to the specified page, so with Sendredirect, the browser's address bar can see changes in address.
With <jsp:forward page= ""/> is different, it is done directly in the server, the browser does not know, also does not deal with the browser, this from the browser address does not change can be seen.

so you need to pay attention to the following two points when using Response.sendredirect:

1, in the use of Response.sendredirect, the front can not have HTML output.

This is not absolute, and the inability to have HTML output actually means that HTML cannot be sent to the browser. In fact, the server now has the cache mechanism, generally in 8K (I mean JSP SERVER), which means that unless you close the cache, or you use the Out.flush () force refresh, then before using Sendredirect, A small amount of HTML output is also allowed.
If the error says, "Some information has been submitted" (the original forgotten), then you should pay attention to see, the front is not too much HTML output.

2, after the Response.sendredirect, should be followed by a return;

We already know that Response.sendredirect is a browser to do the steering, so only after the page processing is complete, there will be actual action. Now that you've got to turn, what's the point of the output? And it is possible that the subsequent output will cause the steering failure.

<%@ include file= "/page/checklogin.jsp"%> the first sentence on the page to be validated.

Checklogin.jsp content is by looking at the session has not logged in the user's content to determine whether to log in, if not, then jump to the landing page:
Copy Code code 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.