一个简单的jsp页面,怎么老是出错!大家帮忙看看啊!

keketian521 2011-10-17 09:11:57
index.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030" pageEncoding="GB18030" import="java.lang.*" %>
<html>
<head>
<title>使用request对象获取请求参数值</title>
</head>
<body>
<%
try{
int money=100;
int number=2;

request.setAttribute("result",money/number);//错误在这里
}catch(Exception e){
request.setAttribute("result","很抱歉,页面出错了。");
}



%>

<jsp:forward page="deal.jsp"/>
</body>

</html>

deal.jsp:
<%@ page language="java" contentType="text/html;charset=GB18030" pageEncoding="GB18030"%>
<html>
<head>
<title>结果页</title>
</head>
<body>
<%
String message=request.getAttribute("result").toString();
out.println(message);

%>


</body>

</html>
...全文
143 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
幽饮烛 2011-10-19
  • 打赏
  • 举报
回复
用的是 1.4 的 JDK 吧,1.4 以上的会自动包装。
康派尔 2011-10-18
  • 打赏
  • 举报
回复
就用包装类吧,Integer类就可以啦。或者干脆直接装箱为Object类型。
kouyiSC 2011-10-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 feng3773216 的回复:]
恩,因为int是数值型,并不是引用类型,所以需要用Integer变成对象型就好了
[/Quote]

合理解释。。。只要对象类型 怎么弄都行。。。
休谱诺斯 2011-10-18
  • 打赏
  • 举报
回复
恩,因为int是数值型,并不是引用类型,所以需要用Integer变成对象型就好了
LoongMint 2011-10-18
  • 打赏
  • 举报
回复
应该是:setAttribute(String,Object);
hellonobody 2011-10-18
  • 打赏
  • 举报
回复
把money/number结果的int类型转成其包装类Integer类型
todaydiy 2011-10-18
  • 打赏
  • 举报
回复
int money=100;
int number=2;

相除的结果还是int型。
jxhxl1990 2011-10-18
  • 打赏
  • 举报
回复
呵呵 接分来的
  • 打赏
  • 举报
回复
楼上都是正确的,除了lz
wangjiangbo2 2011-10-18
  • 打赏
  • 举报
回复
request.setAttribute("result",money/number+"");
把money/number转成String对象就没问题了
火光闪耀 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 stonefeng 的回复:]

request.setAttribute("result",money/number);
标红的地方错了,因为money/number的结果类型是int,但是setAttribute方法无法接受int作为参数的。应该是以对象作为参数,所以可以改成这样:
request.setAttribute("result",new Integer(money/number));
[/Quote]

是这样的setAttribute(string,object);
疯癫行者 2011-10-17
  • 打赏
  • 举报
回复
request.setAttribute("result",money/number);
标红的地方错了,因为money/number的结果类型是int,但是setAttribute方法无法接受int作为参数的。应该是以对象作为参数,所以可以改成这样:
request.setAttribute("result",new Integer(money/number));

81,091

社区成员

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

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