首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法从对象强制转换为布尔值

无法从对象强制转换为布尔值
EN

Stack Overflow用户
提问于 2012-06-04 04:12:31
回答 2查看 29.3K关注 0票数 20

这是我收到的错误,

org.apache.jasper.JasperException: Unable to compile class for JSP: 

    An error occurred at line: 13 in the jsp file: /index.jsp
    Cannot cast from Object to boolean

这是我的代码:

控制器Servlet

if(authentication.verifyCredentials(request.getParameter("username"), 
   request.getParameter("password")))
{
        session.setAttribute("username", request.getParameter("username"));
        session.setAttribute("loggedIn", true);
        dispatcher.forward(request, response);   
}

我也试过了,

session.setAttribute("loggedIn", new Boolean(true));

JSP

<% 
    if(session.getAttribute("loggedIn") != null)
    {
        if(((boolean)session.getAttribute("loggedIn")))
        {
            response.sendRedirect("Controller"); 
        }
    }   
%>

是的,我研究了一下,也看到了之前的堆栈溢出;但是我仍然不能解决我的问题。请协助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-04 04:17:20

尝试将其强制转换为Boolean (可为空),而不是JSP中的boolean

if(((Boolean)session.getAttribute("loggedIn")))
{
    response.sendRedirect("Controller"); 
}
票数 23
EN

Stack Overflow用户

发布于 2012-06-04 04:15:43

尝试使用

   if(((Boolean)session.getAttribute("loggedIn")))

而不是:

   if(((boolean)session.getAttribute("loggedIn")))

属性必须作为Boolean,而不是作为基元类型

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10873595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档