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.