关于JSP与Int不得不说的故事

今天遇到一个问题,我在一个一个jsp中有一个input type="radio",该radio的值放到了session.getAttribute("sex")中,这时候要展示页面的时候需要判断从sql获取的值到底是哪个radio,  so,我写了一下判断

<% if((int)session.getAttribute("sex")==0){%>
<input type="radio" name="ia.sex" value="0" checked="checked"/>男
<input type="radio" name="ia.sex" value="1" />女
<% }else if((int)session.getAttribute("sex")==1){%>
<input type="radio" name="ia.sex" value="0" />男
<input type="radio" name="ia.sex" value="1" checked="checked"/>女
<% }%>

但是这时候一发布发现一个问题!

报错:

 Unable to compile class for JSP:                 

An error occurred at line: 68 in the jsp file: /success/logdata.jsp

Cannot cast from Object to int

什么意思呢?

无法编译Jsp:
行有一个错误:68在JSP文件:/成功/ logdata.jsp
不能投到int的对象

然后我就将int换为Integer,如下

<% if((Integer)session.getAttribute("sex")==0){%>
<input type="radio" name="ia.sex" value="0" checked="checked"/>男
<input type="radio" name="ia.sex" value="1" />女
<% }else if((Integer)session.getAttribute("sex")==1){%>
<input type="radio" name="ia.sex" value="0" />男
<input type="radio" name="ia.sex" value="1" checked="checked"/>女
<% }%>

运行!OK,所以

JSP这个大坑货居然不认识Int!!!

原文地址:https://www.cnblogs.com/zzxxll/p/5485291.html