Solved

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

Posted on 2004-08-27
6
891 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 52

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

By reading this blog, MSPs will gain insight into how to improve communications with their clients as well as establish a more profitable business.
Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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