java - 无法从对象转换为 boolean 值

标签 java jsp servlets session-variables

这是我收到的错误,

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

这是我的代码:

Controller 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"); 
        }
    }   
%>

是的,我研究并看到了以前的 stackoverflow post ;但是我仍然无法解决我的问题。 请帮忙。

最佳答案

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

if(((Boolean)session.getAttribute("loggedIn")))
{
    response.sendRedirect("Controller"); 
}

关于java - 无法从对象转换为 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10873595/

相关文章:

java - arrayList e session jsp的问题

java - Struts 排除带有 Spring 的图案

java - 如何在发送响应后设置 session 变量?

java - Spring Web 应用程序编码

java - 删除 GXT Tabpanel 的默认关闭选项卡选项?

java - Steam 社区好友列表并向他们发送消息

java - Maven依赖管理: Transitive Vs Direct Dependency

java - 改造请求中如何设置界面参数

jquery - 如何在JSP页面中实时显示通知

jsp - 在多部分 enctype 表单中检索多个表单字段参数