Link to home
Start Free TrialLog in
Avatar of padmasambhava
padmasambhava

asked on

Need help understanding using data types with session.setAttribute & session.getAttribute

I'm learning jsp slowly and painfully so please forgive my newbie question...

In one page I'm trying to set a session variable.  The second line here blows up.

    '<% char schoolMode=''a'';
    session.setAttribute("schMode", schMode); %>

And in another I try to access the variable and it blows up as well.
    <% char  schMode=(char)session.getAttribute("schMode"); %>


Thanks a bunch.  
Avatar of ramazanyich
ramazanyich
Flag of Belgium image

It is a typo in your session.setAttribute. It should be
session.setAttribute("schMode",schoolMode);
IT IS

  '<% char schoolMode=''a'';
    session.setAttribute("schMode", schoolMode); %>
OR simply
<%    session.setAttribute("schMode","a");%>

TO ACCESS FIRST/second ONE ,

    <% String  schMode=session.getAttribute("schMode").toString(); %>


-Murali*
Avatar of padmasambhava
padmasambhava

ASKER

Is there a way to store a char value in the session?  I will be going directly into a switch statement, which if I understand correctly only works with char or byte values.  

How do I store the various data types in the session?  It seems klunky to convert everything to string and back.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Session should able to store Objects by default.

You may need to check the type of object store before you do the conversion.

use:

session.getAttribute("myobject").getClass()
><% char  schMode=(char)session.getAttribute("schMode"); %>  
and you must un-box  (which will done automatically in java 1.5).
<% char  schMode=((Character)session.getAttribute("schMode")).charValue(); %>