我是楼主 回复 :#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? 怎么输出结果啊?
绝对能用iterator的,报的什么错
空指针异常: 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.
jsp2 中 List<ChoiceQuestion> list=(List<ChoiceQuestion>)session.getAttribute("cqList");
把5L例子里的session改成request就可以了
用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? 怎么输出结果啊?
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.
request是请求,在一次请求和回复中有效。
session是会话,从你登陆到你登出整个过程有效。
context是servlet容器,整个应用过程有效。
jsp1 中 request.setAttribute("cqList",cq);
jsp2 中 List<ChoiceQuestion> list=(List<ChoiceQuestion>)request.getAttribute("cqList");
这样可以吗?
List list = session.getAttribute("list");
那肯定嘛,request是只有页面有跳转才有用啊。LZ还没搞清request,session的用法。
建议楼主。直接把List<ChoiceQuestion> cq = new ArrayList<ChoiceQuestion>();的值写一个方法返回它(List<ChoiceQuestion>)就是了。什么时候实例就什么时候调用。用session占用资源,影响速度。