急急急:如何用session

DazzlingBeing 2009-06-19 09:43:28
我有2个jsp页面。1.jsp和2.jsp
1.jsp中在<%! %>之间定义了一个vector数组myVector。有很多数据通过1.jsp中的输入获得,数据存入vector。
之后用session.setAttribute("vector", myVector)保存。
然后点击1.jsp中的一个按钮,页面跳转到2.jsp。在2.jsp中用Vector resVector = (Vector)session.getAttribute("vector");结果什么也没有。
我在该语句下面加入 <script language="javascript">
alert('<%= resVector %>');
</script>
也没有弹出alert。

不知道哪里出了问题
...全文
35 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
星火燎猿 2009-06-19
  • 打赏
  • 举报
回复
单引号换成双引号试一下...
gy132 2009-06-19
  • 打赏
  • 举报
回复
这是例子:

这是首页
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
List list=new ArrayList();
list.add("1");
list.add("2");
session.setAttribute("list",list);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.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>
<a href="one.jsp">This is my JSP page. </a><br>
</body>
</html>



这是第二个页面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
List list=new ArrayList();

list=(List)session.getAttribute("list");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.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>
<%for(Object o:list){ %>
<%=o %><br/>
<%} %>
</body>
</html>
jinchun1234 2009-06-19
  • 打赏
  • 举报
回复
应该是没有存进去!是不是<% %>写成<%! %>了
少龙123 2009-06-19
  • 打赏
  • 举报
回复
session.setAttribute("vector", myVector)
你确定放到session里的是有值的吗?debug一下
bingsha1976 2009-06-19
  • 打赏
  • 举报
回复
session.setAttribute("vector", myVector)

看myVector是否为null
JackRui2008 2009-06-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kadach11 的回复:]
给LZ个建议,JSP最好只是用来显示数据的,至于逻辑方面可以考虑放到后台执行,这样可以方便调试。可以考虑用JSTL,

自定义标签等等。养成好的编码风格。
[/Quote]


说的很对,lz这样写不容易调试,出现错误很难分清是哪里错了。
你先确定session.setAttribute("vector", myVector) 里的vector是否得到了结果,
没有弹出alert,因为你点击按钮的时候,并没有走这个js,请确认你的按钮onclick事件是否写的正确。另外,你可以在js事件里先写一个简单的弹出语句,如先alert(“hello”),看看是否进入,有的时候你要注意onclick的名字,有的时候名字起错了(如关键字),也不走的,最好先起个英文带数字的如atest123等。
kadach11 2009-06-19
  • 打赏
  • 举报
回复
给LZ个建议,JSP最好只是用来显示数据的,至于逻辑方面可以考虑放到后台执行,这样可以方便调试。可以考虑用JSTL,

自定义标签等等。养成好的编码风格。
fosjos 2009-06-19
  • 打赏
  • 举报
回复
可能是页面缓存,tomcat删除work目录下的文件试试
不行的话,在jsp里加上去缓存的代码
  • 打赏
  • 举报
回复
7楼好样的
晓悦 2009-06-19
  • 打赏
  • 举报
回复
喵!

81,094

社区成员

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

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