日期:2014-05-20  浏览次数:20817 次

java.lang.IllegalStateException。请求帮助
写了个Servlet,当request.getParameter("num") 中传递来的num是一个非数字时,我觉得应该catch到后跳转到response.sendRedirect("sysError.html");了,可为什么会报
request.getRequestDispatcher("viewCart.jsp").forward(request, response);有错java.lang.IllegalStateException
这个错我也看不懂是啥意思。

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=gbk");
request.setCharacterEncoding("gbk");
HttpSession session = request.getSession();
Map<String,CartItemBean> cart = (HashMap<String, CartItemBean>)session.getAttribute("cart");
try {
String isbn = request.getParameter("isbn");
int num = Integer.parseInt(request.getParameter("num"));
CartItemBean item = cart.get(isbn);
if(item.getQuantity() - num >= 1){
item.setQuantity(item.getQuantity()-num);
}
else{
cart.remove(isbn);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
response.sendRedirect("sysError.html");
}
request.getRequestDispatcher("viewCart.jsp").forward(request, response);
}


------解决方案--------------------
catch最后加个return;