20

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.

Community
  • 1
  • 1
Dennis
  • 3,962
  • 7
  • 26
  • 44

2 Answers2

23

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

if(((Boolean)session.getAttribute("loggedIn")))
{
    response.sendRedirect("Controller"); 
}
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • 6
    Remember, all the lowercase types are built-in primitive types not extending `Object`. If you want to use them with references, you need to use the uppercase versions, which are "boxed" types. – Wormbo Jun 03 '12 at 20:26
  • Thank you for handy tip! Onward I can't forget 'Boolean' :) – mumair Oct 22 '15 at 09:00
8

try with

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

instead of:

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

attribute has to be taken as Boolean, not as primitive type

dantuch
  • 9,123
  • 6
  • 45
  • 68