Solved

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

Posted on 2004-08-27
6
888 Views
Last Modified: 2010-04-01
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.  
0
Comment
Question by:padmasambhava
6 Comments
 
LVL 19

Expert Comment

by:ramazanyich
ID: 11919686
It is a typo in your session.setAttribute. It should be
session.setAttribute("schMode",schoolMode);
0
 
LVL 13

Expert Comment

by:Murali Murugesan
ID: 11920649
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*
0
 

Author Comment

by:padmasambhava
ID: 11920935
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
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Accepted Solution

by:
rrz earned 100 total points
ID: 11921216
><% char schoolMode=''a'';
    session.setAttribute("schMode", schMode); %>
You need to box up that primitive( In java 1.5 will be done automatically).
<% char schoolMode=''a'';
    session.setAttribute("schMode", new Character(schoolMode)); %>


0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 11921226
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()
0
 
LVL 27

Expert Comment

by:rrz
ID: 11921232
><% 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(); %>
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Display current date,month,and year to JTextField 3 59
immutable object concept 5 127
iterator example remove 8 75
ejb on wildfly 5 30
This article describes how to reset your Windows 10 password when you've forgotten it.
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question