日期:2014-05-18  浏览次数:20765 次

request.setAttribute和request.getAttribute的问题
请问,ACTION里request.setAttribute方法和JSP中的request.getAttribute方法之间只能传递字符串,而传递INT或其他类型都get不到是怎么回事,谢谢各位    


注释:只能传递字符串举例
request.setAttribute( "name ", "jack ")这样可以通过request.getAttribute方法在JSP中得到
而request.setAttribute( "age ",18)这样传递int型或其他类型的参数在JSP中用request.getAttribute方法就得不到了
报的错误为:
ServletException   during   processing   of   content:   (   java.lang.Integer)   [60712]   Caused   by:     Caused   by:   java.lang.ClassCastException   java.lang.Integer
java.lang.ClassCastException:   java.lang.Integer
java.lang.ClassCastException:   java.lang.Integer

------解决方案--------------------
可能是你的JDK版本不够高,换成5.0的应该没问题。

你可以这样试试。

request.setAttribute( "age ",18)改成request.setAttribute( "age ",new Integer(18))
------解决方案--------------------
类型转换的问题,转一下就可以
------解决方案--------------------
数据类型不符合,你就转换成你需要的类型!!!
------解决方案--------------------
request.setAttribute( "age ",18);

int i=((Integer)request.getAttribute( "age ")).intValue();

------解决方案--------------------
request.setAttribute( "age ",new Integer(18));
這麽寫肯定沒問題
------解决方案--------------------
setAttribute和getAttribute只能传递对象,不能传递基本类型,只能把基本类型转换成封装类在进行传递。