Cannot cast from Object to boolean

Dennis picture Dennis · Jun 3, 2012 · Viewed 28.6k times · Source

This is the error I am receiving,

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

This is my code:

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);   
}

I also tried this,

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

JSP

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

Yes I researched and also saw the previous stackoverflow post; however I still cannot resolve my problem. Please assist.

Answer

ChristopheD picture ChristopheD · Jun 3, 2012

Try casting it to Boolean (nullable) instead of boolean in the JSP:

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