请问下jsp存值和取值

来源:百度知道 编辑:UC知道 时间:2024/06/15 09:35:47
<%
request.setAttribute("Customer ID",1);
request.setAttribute("Ship To ID",2);
%>

<%
int customerId = request.getAttribute("Customer ID");
int shipToId = request.getAttribute("Ship To ID");
%>

现在这个取值 出错了
不能在本页存 本页取 来测的么?
找到: java.lang.Object
需要: int
int customerId =(int)request.getAttribute("Customer ID");

<%
int customerId =(int)request.getAttribute("Customer ID");
int shipToId =(int)request.getAttribute("Ship To ID");
%>

我把这个 放在<html> 前面 应该没问题吧

3楼的朋友写的方法是对的 我想再请问一个问题就是
getAttribute 是只能用一次的 如何避免 如果没有传值过来 这边不运行呢 现在就是 如果没有set 而这边直接get的话 是会出错的

你代码有错误,Object类型的对象不能被直接转换成 int类型.
int customerId = Integer.valueOf(request.getAttribute("Customer ID").toString());

这样才是正确的转换方式.

加判断啊。
int customerId = 0; //给一个初始值
if(request.getAttribute("Customer ID")!=null){ //判断不为null,没set所以为null
customerId = Integer.valueOf(request.getAttribute("Customer ID").toString());
}

你再本页上 set 本页get 如果没有获得 是有毛病 你要加断点看看
如果不是本页面传值 需要你指定跳转啊

Object类型的对象不能被直接转换成 int类型.
int customerId = Integer.valueOf(request.getAttribute("Customer ID").toString());

----------------------------
你根据这个试试,看能不能取到数据输出
if(null==request.getAttribute("Customer ID"))
{
System.out.println("在本页取数据ERROR!");
}else
{
System.out.println("你太有才了");
}

变量名里面可以出现空格?