日期:2014-05-17  浏览次数:20704 次

关于接收servlet中session传过来的值!
 servlet部分
                QueryGood qg = new QueryGood();
List<Good> allgoodlist = qg.querygood(goodtype, goodname);
HttpSession session = request.getSession(true);
request.setAttribute("allgoodlist", allgoodlist);
RequestDispatcher dispatcher = request.getRequestDispatcher("cart.jsp");
dispatcher.forward(request, response);
 jsp部分 我在cart.jsp是可以接收传过来的值的。
          
但是我在另一个querygood.jsp页面想接收的时候 <%
   List list = (List)request.getAttribute("allgoodlist");
   Iterator its = list.iterator();  //报空指针异常
   while(its.hasNext()) {
   Good good = new Good();
  good = (Good)its.next();
      
%>
包我也已经导入了。
请问有哪位大神知道原因吗?

------解决方案--------------------
request.setAttribute("allgoodlist", allgoodlist)

你这样是用的 request,没用 session。
根据作用域,所以 cart.jsp 能收到,而 querygood.jsp 收不到。
你应该用 session.setAttribute 传值,而用 session. getAttribute 接数据,就可以了。
------解决方案--------------------
2楼正解,request在当前请求范围内有效,楼主的情况已经不在当前request作用域范围内了,就拿不到值了。换session吧