把vector对象保存在session中,为什么到了另一个页面,取出vector对象却报错!

hawaii_wx 2005-08-23 10:08:16
如题:
我写了个购物车的简单例子,一路走过来的那个难呀!
写道最后了,还是遇到个我自己解决不了的问题
我在写到购买的时候,想把购买一次商品的过程(例如把商品名,商品数量,本次的总价)放进一个vector中,然后把这个vector放入session中,这样,我在结算页面就很容易的取出来了
请大家看看我的代码:

balance.jsp代码:
<%@ page contentType="text/html;charset=gbk"%>
<%@ page language="java" import="java.util.*" %>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'balance.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
结算页. <br>
<jsp:useBean id="spg" scope="page" class="login.Shopping"/>
<%//防止某些用户不进行登陆,直接想进入此页面
if (session.getAttribute("userName")==null){
response.sendRedirect("errorhandler.jsp");
}%>

<%
spg.setShoppingID(Integer.parseInt(request.getParameter("shoppingID")));
if(spg.executeBuy())
{
try
{
while(spg.rs.next())
{
String str=spg.rs.getString("shoppingName");
str=new String(str.getBytes("ISO-8859-1"),"gbk");
out.println("恭喜"+session.getAttribute("userName"));
out.println("您在本站购买了商品"+str);
out.println("购买的数量为:"+request.getParameter("count"));
float money=Integer.parseInt(request.getParameter("count"))*spg.rs.getFloat("shoppingPrice");
out.println("本次花费"+money+"RMB");
String spgName=str;

Vector v=new Vector();
v.add(spgName);//添加商品名
v.add(request.getParameter("count"));//添加数量
v.add(String.valueOf(money));//添加本次总价
session.setAttribute(spgName,v);//把vector添加到session中

}
}
catch(SQLException e)
{
out.println("结算页错误:"+e.getMessage());
}
}
%>
<a href="last.jsp">不买了,结帐</a>
<br>
<p><a href="index.jsp">继续购买</a></p>
</body>
</html>

last.jsp页面代码
<%@ page contentType="text/html;charset=gbk"%>
<%@ page language="java" import="java.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'last.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
最后页面. <br>
<%//防止某些用户不进行登陆,直接想进入此页面
if (session.getAttribute("userName")==null){
response.sendRedirect("errorhandler.jsp");
}%>
<%

out.println("尊敬的客户:"+session.getAttribute("userName")+" 您在本站购买了如下商品:<br>");
Enumeration e=session.getAttributeNames();
String s[]=session.getValueNames();
for(int i=0;i<s.length;i++)
{
out.println(s[i]);
}
out.println("<<<<>>>>");
while(e.hasMoreElements())
{
String str=(String)e.nextElement();
if(str.equals("userName"))
{
out.println(e.nextElement());
Vector v=(Vector)(session.getAttribute((String)e.nextElement()));//就是这行不认,报错是java.util.NoSuchElementException这个错误,nnd,明明是可以的呀,我的session从头到尾就放了个“userName”,在就是vector了,唉,郁闷哦

if(!v.isEmpty())
{
Iterator it=v.iterator();
while(it.hasNext())
{
out.println(it.next());
//out.println("商品名称:"+v.get(1));
//out.println("商品数量:"+v.get(2));
//out.println("该商品总价:"+v.get(3)+"<br>");
}
}
else
{
out.println("vector是空的");
}
}

}
session.invalidate();
%>
</body>
</html>

还请大家帮帮忙哦
...全文
205 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hawaii_wx 2005-08-23
  • 打赏
  • 举报
回复
总算在大家的帮助下,搞定了,现在结贴
hawaii_wx 2005-08-23
  • 打赏
  • 举报
回复
哦买嘎的
搞定了
把代码贴出来看看:
<%

out.println("尊敬的客户:"+session.getAttribute("userName")+" 您在本站购买了如下商品:<br>");
Enumeration e=session.getAttributeNames();

while(e.hasMoreElements())
{
String str=(String)e.nextElement();
if(!str.equals("userName"))
{

Vector v=(Vector)(session.getAttribute(str));
if(!v.isEmpty())
{
out.println("商品名称:"+v.get(0));
out.println(" 商品数量:"+v.get(1));
out.println(" 该商品总价:"+v.get(2)+"<br>");
}
else
{
out.println("vector是空的");
}
}

}
session.invalidate();
%>
这里改了,其他的地方都没改
hawaii_wx 2005-08-23
  • 打赏
  • 举报
回复
楼上的哥们,我放进去的Attribute我确实不知道是什么,动态生成的,请看你上面一楼我写的注释部分
用Enumeration,是当时看到session.getAttributeNames()这个东东返回Enumeration,没多想
呵呵
现在问题基本已经搞定,都是那个out.println(e.nextElement());惹的祸
jaopen 2005-08-23
  • 打赏
  • 举报
回复
我有一点一直很纳闷:你为什么非要用Enumeration保存session.getAttributeNames();难道你自己放进去的Attribute,你还不知道names吗,而且你又不需要对Enumeration的每一个进行遍历,你要getAttribute,直接传入预先定义的字符串不就得了。
hawaii_wx 2005-08-23
  • 打赏
  • 举报
回复
各位:
Vector v=new Vector();
v.add(spgName);//添加商品名
v.add(request.getParameter("count"));//添加数量
v.add(String.valueOf(money));//添加本次总价
session.setAttribute(spgName,v);//把vector添加到session中
这里session放入的属性名是动态的,由商品名决定
while(e.hasMoreElements())
{
String str=(String)e.nextElement();
if(!str.equals("userName"))//把sessiion中“userName”这个属性除外
{
out.println(e.nextElement());
Vector v=(Vector)(session.getAttribute(str));//这里改了
if(v.isEmpty())
{
Iterator it=v.iterator();
while(it.hasNext())
{
out.println(it.next());
//out.println("商品名称:"+v.get(1));
//out.println("商品数量:"+v.get(2));
//out.println("该商品总价:"+v.get(3)+"<br>");
}
}
else
{
out.println("vector是空的");
}
}

}

但是我改成这样,结果还是报那个java.util.NoSuchElementException错
cuilupeng999 2005-08-23
  • 打赏
  • 举报
回复
就是你取了两次nextElement()啊,才报出这个错了,可以把out.println(e.nextElement())去掉试试
jaopen 2005-08-23
  • 打赏
  • 举报
回复
String str=(String)e.nextElement();//这一行代码已经在Enumeration e中取了一次元素
if(str.equals("userName"))
{
out.println(e.nextElement());//这一行代码又在Enumeration e中取了一次元素
Vector v=(Vector)(session.getAttribute((String)e.nextElement()));//就是这行不认,报错是java.util.NoSuchElementException这个错误,nnd,明明是可以的呀,我的session从头到尾就放了个“userName”,在就是vector了,唉,郁闷哦-----据你自己所言:你的Enumeration e中应该只有两个元素,到第三次取的时候Enumeration到底了,当然是NoSuchElementException了。
hawaii_wx 2005-08-23
  • 打赏
  • 举报
回复
楼上的哥们,我去看看呀,看来又是犯了个低级错误,
jihanzhong 2005-08-23
  • 打赏
  • 举报
回复
out.println(e.nextElement());//这里next()了
Vector v=(Vector)(session.getAttribute((String)e.nextElement()));//这里又next()

而你只有一个元素~。。。。。。
hawaii_wx 2005-08-23
  • 打赏
  • 举报
回复
请大家看看,这种情况如果不放入vector还有什么其他的好办法?
推荐下,大家一起研究呀
sep11 2005-08-23
  • 打赏
  • 举报
回复
不会,帮你up
hawaii_wx 2005-08-23
  • 打赏
  • 举报
回复
楼上的,佩服你!
问下,你的jsp里面放什么东东?
angues1980 2005-08-23
  • 打赏
  • 举报
回复
这个看着就头晕,我从来不在jsp里面放脚本
不过还是帮你UP~
hawaii_wx 2005-08-23
  • 打赏
  • 举报
回复
今天早上是怎么回事,老是发贴不成功!

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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