A little practical use of response.sendredirect

Source: Internet
Author: User
Redirect|response

< black Coffee original >
The following is a JSP page for a shopping list submission (correct)

<%@ page contenttype= "text/html" language= "java" import= "java.util.*,com.blackcoffee.shop.*, Com.blackcoffee.util.*,com.blackcoffee.db.* "%>
<%@ page errorpage= "/error.jsp"%>
<%
if (Associatorsession.getsession (session) ==null)
Response.sendredirect ("/login.jsp"); If the member is not logged in to jump to the login page,
else{///If you are already logged in, submit your order and destroy the shopping cart in session
Associator Associator = new Associator ();
Associator = associatorsession.getsession (session);
String errmsg = "";
Errmsg=orderformoperation.addorderform (request,session);
Cartsession.removesession (session); Destroy the shopping cart in session
if (!errmsg.equals (""))
Response.sendredirect ("/error.jsp?errmsg=" +errmsg);
%>


<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<script language=javascript>
Alert ("The above products have been ordered, we will contact you in time!" ");
Window.location= ". /index.jsp ";
</Script>
</body>

<%}%>

The code with the problem is this

<%@ page contenttype= "text/html" language= "java" import= "java.util.*,com.blackcoffee.shop.*, Com.blackcoffee.util.*,com.blackcoffee.db.* "%>
<%@ page errorpage= "/error.jsp"%>
<%
if (Associatorsession.getsession (session) ==null)
Response.sendredirect ("/login.jsp"); If the member is not logged in to jump to the login page,
//No more else.
Associator Associator = new Associator ();
Associator = associatorsession.getsession (session);
String errmsg = "";
Errmsg=orderformoperation.addorderform (request,session);
Cartsession.removesession (session); Destroy the shopping cart in session
if (!errmsg.equals (""))
Response.sendredirect ("/error.jsp?errmsg=" +errmsg);
%>


<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<script language=javascript>
Alert ("The above products have been ordered, we will contact you in time!" ");
Window.location= ". /index.jsp ";
</Script>
</body>

<%//}%>

Please note that the problem arises because there is no else, the problem is as follows: Every time you do not log in on the purchase of items, point "order" submit button, should have to go to the login page, to view the shopping cart, before the purchase of goods should be in, but the point "order" submit button, turn to the landing page, the shopping cart after login. At that time the error seems to say that Response.sendredirect has been sent to the client, can not be changed, I was here to toss some time, and then check the code found this sentence cartsession.removesession (session);

Original if (Associatorsession.getsession (session) ==null)
Response.sendredirect ("/login.jsp"); If the member is not logged in to jump to the login page,

This sentence is not followed by else, so the page, although the jump, but the following statement also continued to execute, cartsession.removesession (session);

It empties the cart, so it makes a mistake.

Summary: After the response.sendredirect jump, the original page of the statement will continue to execute.



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.