如何传递数组参数?

hanyl 2003-06-24 05:14:39
我在一页面1.jsp中有这样一个脚本,
<% for(int i=0;i<12;i++){
int ID=1 +(int)(Math.random()*12);
questionid[i]=ID;
%>
想在2.jsp中获得数组questionid的值,如何通过参数传递在2.jsp中取得该数组参数?
我是这样做的,结果报错:
1.jsp中:<input type="hidden" name="strPara" value="<%=questionid%>">
2.jsp中:String[] questionId = request.getParameterValues("strPara");
请问该如何实现数组参数的传递?
...全文
1477 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
biggie 2003-06-25
  • 打赏
  • 举报
回复
不过想清空还是建议你用
session.removeAttribute("strPara");
biggie 2003-06-25
  • 打赏
  • 举报
回复
session 是有生命周期的,你可以在tomcat中的web.xml中设置
也可以自己用session.setMaxInactiveInterval(int);设置

如果想清空所有session可以用session.invalidate();
如果只想清空这个,那就用
session.setAttribute("strPara", null);
hanyl 2003-06-25
  • 打赏
  • 举报
回复
我试了,request.getParameterValues("strPara") 不能取出数组参数值,除非上页传递的是checkbox的多选值
Ortega111 2003-06-25
  • 打赏
  • 举报
回复
你试试
String[] questionId = request.getParameterValues("strPara") == null ? "" : request.getParameterValues("strPara") ;
hanyl 2003-06-25
  • 打赏
  • 举报
回复
我用的biggie(飞碟)的方法:
传出的页面:
<%session.setAttribute("strPara", questionid);%>
接收的页面:
<%
String[] questionIds = (String[])session.getAttribute("strPara");
%>
但是好像用<%request.setAttribute("strPara", questionid);%>是不行的,但是这种局部建立的session是否应该在适当的时候及时停掉呢?如何做?
hanyl 2003-06-25
  • 打赏
  • 举报
回复
谢谢biggie(飞碟),测试用<%session.setAttribute("strPara", questionid);%>实现传递数组参数成功。多谢帮忙。
另外,如果谁有其他方法可以实现数组参数传递的,也请留步和留言。
xutao1981 2003-06-24
  • 打赏
  • 举报
回复
同意楼上的

anfield 2003-06-24
  • 打赏
  • 举报
回复
先不要把selectSymptomId[i]转换为Integer,String传过去,反正传过去还要转换回Integer,何必多此一举。
Rangiggs 2003-06-24
  • 打赏
  • 举报
回复
你是用什么方法转到第二个页面的啊?
如用response.sendRedirect("xxx.jsp"); 一定接受不到的request.setAttribute传送的东西的
改用<jsp:forward page="xxx.jsp"/>
hanyl 2003-06-24
  • 打赏
  • 举报
回复
传出页面:selectSymptomIdString是从上歌页面的checkbox传来,我测试过了这个传参是正确的
String[] selectSymptomIdString = request.getParameterValues("actionSelect");
///*将数组转换成整形
int[] selectSymptomId = new int[selectSymptomIdString.length];
for(int i=0;i<selectSymptomIdString.length;i++){
selectSymptomId[i] = Integer.parseInt(selectSymptomIdString[i]);
}
//*/将数组转换成整形
out.println(selectSymptomIdString.length);
request.setAttribute("strPara", selectSymptomIdString);
接受页面:
String[] strSymptomIdString = (String[])request.getAttribute("strPara");
int[] strSymptomId = new int[strSymptomIdString.length];
for(int i=0;i<strSymptomIdString.length;i++){
strSymptomId[i] = Integer.parseInt(strSymptomIdString[i]);
}
biggie 2003-06-24
  • 打赏
  • 举报
回复
把你的代码贴出来

你查看一下你取出的值是否为空的
hanyl 2003-06-24
  • 打赏
  • 举报
回复
我试了上面各位的方法,但是还是报错:java.lang.NullPointerException
:(
ZeroC 2003-06-24
  • 打赏
  • 举报
回复
request.setAttribute("strPara", questionid);
String[] questionIds = (String[])request.getAttribute("strPara");

没有必要不要用session
你所写的那种只在checkbox情况才可用,hidden是不行的,一个name只能有一个值
biggie 2003-06-24
  • 打赏
  • 举报
回复
传出的网页:
<%request.setAttribute("strPara", questionid);%>
接收的页面:
<%
String[] questionIds = (String[])request.getAttribute("strPara");
%>
or
<%session.setAttribute("strPara", questionid);%>
接收的页面:
<%
String[] questionIds = (String[])session.getAttribute("strPara");
%>
truss 2003-06-24
  • 打赏
  • 举报
回复
我觉得应该用session来解决这个问题
因为现在你是在两个不同的jsp下
whatawalter 2003-06-24
  • 打赏
  • 举报
回复
传出的网页:
<%request.setAttribute("strPara", questionid);%>
接收的页面:
<%
String[] questionIds = (String[])request.getAttribute("strPara");
%>

81,094

社区成员

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

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