哪位大虾告诉我这个怎么解决啊:Cannot forward after response has been committed
我是个JSP的初学者,今天碰到个问题非常头疼。。页面不能跳转,数据提交到数据库正常,出个这样的错:java.lang.IllegalStateException: Cannot forward after response has been committed
它的意思好象是不能在响应提交之后跳转。。。怎么都搞不好,望各位高手帮忙出出主义
代码如下:
package bookshop;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class submitServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
request.setCharacterEncoding( "GBK ");
response.setCharacterEncoding( "GBK ");
HttpSession session1 = request.getSession(false);
orderBean order = (orderBean) session1.getAttribute( "order ");
String LoginName = (String) session1.getAttribute( "LoginName ");
Integer number1;
String pay1 = request.getParameter( "pay ");
String carry1 = request.getParameter( "carry ");
String bz1 = request.getParameter( "bz ");
order.setPay(pay1);
order.setCarry(carry1);
order.setBz(bz1);
String username = order.getUserName();
System.out.println(username);
String address = order.getAddress();
String postcode = order.getPostCode();
System.out.println(postcode);
String usertel = order.getUserTel();
String mobiletel = order.getMobileTel();
String email = order.getEmail();
String pay = order.getPay();
String carry = order.getCarry();
String bz = order.getBz();
System.out.println(bz);
try {
DBBean dbconn = new DBBean();
String sql1 = "insert into orderinfo (LoginName,UserName,Address,PostCode,UserTel,MobileTel,Email,Pay,Carry,Bz) values ( ' " +
LoginName + " ', ' " + username + " ', ' " + address +
" ', ' " + postcode + " ', ' " + usertel + " ', ' " +
mobiletel + " ', ' " + email + " ', ' " + pay + " ', ' " +
carry + " ', ' " + bz + " ') ";
String sql2 =
"select top 1 orderid from orderinfo where loginname= ' " +
LoginName + " ' order by orderdate desc ";
if (dbconn.execute(sql1)) {
ResultSet rs = dbconn.executeQuery(sql2);
rs.next();
long orderid = rs.getLong( "orderid ");
dbconn.close();
HashMap itemMap = (HashMap) session1.getAttribute( "itemMap ");
Iterator it = itemMap.keySet().iterator();
while (it.hasNext()) {
String bookID = (String) it.next();
DBBean dbconn1 = new DBBean();
ResultSet rs2 = dbconn1.executeQuery(
"select * from bookInfo where BookID= ' " +
bookID + " ' ");
if (rs2.next()) {
float memberPrice = rs2.getFloat( "MemberPrice ");
number1 = (Integer) itemMap.get(bookID);
int number = number1.intValue();
String sql4 =
"insert into order_ditail (OrderID,BookID,Price,Number) values ( " +
orderid + ", ' " + bookID + " ', " + memberPrice +
", " + number + ") ";
dbconn1.close();
DBBean dbconn2 = new DBBean();
if (dbconn2.execute(sql4)) {
session1.removeAttribute( "itemMap ");
javax.servlet.RequestDispatcher sumbit = request.
getRequestDispatcher( "/sesses.jsp ");
sumbit.forward(request, response);
} else {
javax.servlet.RequestDispatcher sumbit = request.
getRequestDispatcher( "/unsess.jsp ");
sumbit.forward(request, response);
}
} else {
javax.servlet.RequestDispatcher sumbit = request.
getRequestDispatcher( "/unsess.jsp ");
sumbit.forward(request, response);
}
}
} else {
javax.servlet.RequestDispatcher sumbit = request.
getRequestDispatcher( "/unsess.jsp ");
sumbit.forward(request, response);
}
} catch (SQLException ex) {
ex.printStackTrace();
javax.servlet.RequestDispatcher sumbit = request.
getRequestDispatcher( "/unsess.jsp ");
sumbit.forward(request, response);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
javax.servlet.RequestDispatcher sumbit = request.
getRequestDispatcher( "/unsess.jsp ");
sumbit.forward(request, response);
}
}
//Clean up resources
public void destroy() {
}
}
[解决办法]
每个 sumbit.forward(request, response);
后面加上return 看看