我在一个jsp页面写了一个List<ChoiceQuestion> cq = new ArrayList<ChoiceQuestion>();我想在另一个jsp页面取出这个list里面的所有值,不知道是不是用session ? 高手们帮帮忙,着急!!! 

解决方案 »

  1.   

    问题补充:如果是用session,希望能具体说一下,谢谢!
      

  2.   

    你是想一个request 返回一个页面  ——》然后点击另一个页面的时候再拿到这些值吗 可以链接传值 , 不过既然数据可以保留在服务器,那我觉得还是放在session方便一些,  暂且忽略效率!
      

  3.   

    jsp1 中 session.setAttribute("cqList",cq);
    jsp2 中 List<ChoiceQuestion> list=(List<ChoiceQuestion>)session.getAttribute("cqList");
      

  4.   

    我觉得还是用request比较好,只是在页面里传一下,又不是在整个会话中都要用到
    把5L例子里的session改成request就可以了
      

  5.   

    我是楼主  回复 :#6楼
      用session得到的结果正确, 我也改成request试了一下:   List<ChoiceQuestion> cq = (List<ChoiceQuestion>)request.getAttribute("cqList");
       <%
         int x = 1;
         for (Iterator<ChoiceQuestion> it = cq.iterator(); it.hasNext();) {// 这句报错
    ChoiceQuestion c = it.next();
        %>
          <tr>
    <td><%=x++%>.<%=c.getId()%></td>
         </tr>
        <%
        }
       %>
        
        我想用iterator去取cq里面的值,结果:
        for (Iterator<ChoiceQuestion> it = cq.iterator(); it.hasNext();){}这句话有问题,是不是不能用iterator? 怎么输出结果啊?
      

  6.   

    绝对能用iterator的,报的什么错
      

  7.   

    空指针异常: page /exam/examEnd.jsp at line 27
    org.apache.jasper.JasperException: An exception occurred processing JSP page /exam/examEnd.jsp at line 2724:   
    25:    List<ChoiceQuestion> cq = (List<ChoiceQuestion>)request.getAttribute("cqList");
    26:    out.println("提交选择题答案");
    27:  for (Iterator<ChoiceQuestion> it = cq.iterator(); it.hasNext();) {
    28:  ChoiceQuestion c = it.next();
    29:  String sc = String.valueOf(c.getId());
    30:  String choice = request.getParameter(sc);
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    root cause java.lang.NullPointerException
    org.apache.jsp.exam.examEnd_jsp._jspService(examEnd_jsp.java:85)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.
      

  8.   

    问题补充: 把request改为session就没上面这个错误了.感觉是和iterator有关系!
      

  9.   

    谁能解释下request的生命周期?
      

  10.   

    session和request的作用域不同page指的是当前页面,在当前页面有效。
    request是请求,在一次请求和回复中有效。
    session是会话,从你登陆到你登出整个过程有效。
    context是servlet容器,整个应用过程有效。
      

  11.   

    request取到的值是null吧,request的生命周期是一起请求有效,即在页面请求到服务器有效,或者在服务器里赋值,传到页面有效
      

  12.   

    request不能在两个jsp页面传值吗
      

  13.   

    比如:
    jsp1 中 request.setAttribute("cqList",cq);
    jsp2 中 List<ChoiceQuestion> list=(List<ChoiceQuestion>)request.getAttribute("cqList");
    这样可以吗?
      

  14.   

    request是可以解决你的那个问题的,建议用request,慢慢的你就回发现它很好用的!尽量避免使用session
      

  15.   

    可是用request不知道为什么会出现空指针异常,而把request改为session后就正常了,纠结!
      

  16.   

    哟西。。session.setAttribute("list",list);
    List list = session.getAttribute("list");
      

  17.   


    那肯定嘛,request是只有页面有跳转才有用啊。LZ还没搞清request,session的用法。
    建议楼主。直接把List<ChoiceQuestion> cq = new ArrayList<ChoiceQuestion>();的值写一个方法返回它(List<ChoiceQuestion>)就是了。什么时候实例就什么时候调用。用session占用资源,影响速度。
      

  18.   

    试试这种request.getSession.setAttribute("cqList",cq);