在不同的两个JSP页面如何传递 " List" 的问题?

wangyongfei2010 2011-10-26 10:46:58
我在一个jsp页面写了一个List<ChoiceQuestion> cq = new ArrayList<ChoiceQuestion>();我想在另一个jsp页面取出这个list里面的所有值,不知道是不是用session ? 高手们帮帮忙,着急!!!
...全文
355 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuxiaoke2009 2011-10-28
  • 打赏
  • 举报
回复
学习了哈!
  • 打赏
  • 举报
回复
哟西。。session.setAttribute("list",list);


List list = session.getAttribute("list");
CloudX2019 2011-10-28
  • 打赏
  • 举报
回复
说用request的都是二愣子
muyipan 2011-10-28
  • 打赏
  • 举报
回复
试试这种request.getSession.setAttribute("cqList",cq);
shenhua 2011-10-28
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 wangyongfei2010 的回复:]
引用 17 楼 k010010001 的回复:
request是可以解决你的那个问题的,建议用request,慢慢的你就回发现它很好用的!尽量避免使用session

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

那肯定嘛,request是只有页面有跳转才有用啊。LZ还没搞清request,session的用法。
建议楼主。直接把List<ChoiceQuestion> cq = new ArrayList<ChoiceQuestion>();的值写一个方法返回它(List<ChoiceQuestion>)就是了。什么时候实例就什么时候调用。用session占用资源,影响速度。
pipi7772 2011-10-27
  • 打赏
  • 举报
回复
谁能解释下request的生命周期?
Tinker77 2011-10-27
  • 打赏
  • 举报
回复
jsp1 中 session.setAttribute("cqList",cq);
jsp2 中 List<ChoiceQuestion> list=(List<ChoiceQuestion>)session.getAttribute("cqList");
wangyongfei2010 2011-10-27
  • 打赏
  • 举报
回复
问题补充: 把request改为session就没上面这个错误了.感觉是和iterator有关系!
wangyongfei2010 2011-10-27
  • 打赏
  • 举报
回复
空指针异常: page /exam/examEnd.jsp at line 27


org.apache.jasper.JasperException: An exception occurred processing JSP page /exam/examEnd.jsp at line 27

24:
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.

funfenffun 2011-10-27
  • 打赏
  • 举报
回复
绝对能用iterator的,报的什么错
wangyongfei2010 2011-10-27
  • 打赏
  • 举报
回复
我是楼主 回复 :#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? 怎么输出结果啊?
wangyongfei2010 2011-10-27
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 k010010001 的回复:]
request是可以解决你的那个问题的,建议用request,慢慢的你就回发现它很好用的!尽量避免使用session
[/Quote]
可是用request不知道为什么会出现空指针异常,而把request改为session后就正常了,纠结!
bike_j2ee 2011-10-27
  • 打赏
  • 举报
回复
建议使用request
funfenffun 2011-10-27
  • 打赏
  • 举报
回复
我觉得还是用request比较好,只是在页面里传一下,又不是在整个会话中都要用到
把5L例子里的session改成request就可以了
k010010001 2011-10-27
  • 打赏
  • 举报
回复
request是可以解决你的那个问题的,建议用request,慢慢的你就回发现它很好用的!尽量避免使用session
wangyongfei2010 2011-10-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 withfox 的回复:]
引用 10 楼 wangyongfei2010 的回复:
空指针异常: page /exam/examEnd.jsp at line 27


org.apache.jasper.JasperException: An exception occurred processing JSP page /exam/examEnd.jsp at line 27

24:
25: List<C……
[/Quote]
比如:
jsp1 中 request.setAttribute("cqList",cq);
jsp2 中 List<ChoiceQuestion> list=(List<ChoiceQuestion>)request.getAttribute("cqList");
这样可以吗?
wangyongfei2010 2011-10-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 withfox 的回复:]
引用 10 楼 wangyongfei2010 的回复:
空指针异常: page /exam/examEnd.jsp at line 27


org.apache.jasper.JasperException: An exception occurred processing JSP page /exam/examEnd.jsp at line 27

24:
25: List<C……
[/Quote]
request不能在两个jsp页面传值吗
withfox 2011-10-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wangyongfei2010 的回复:]
空指针异常: page /exam/examEnd.jsp at line 27


org.apache.jasper.JasperException: An exception occurred processing JSP page /exam/examEnd.jsp at line 27

24:
25: List<ChoiceQuestion> cq = (List<Choi……
[/Quote]
request取到的值是null吧,request的生命周期是一起请求有效,即在页面请求到服务器有效,或者在服务器里赋值,传到页面有效
huangmd 2011-10-27
  • 打赏
  • 举报
回复
session和request的作用域不同

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

yangzhijiang675 2011-10-26
  • 打赏
  • 举报
回复
直接把list放里面啊
加载更多回复(3)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧