Solved

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

Posted on 2004-08-27
6
877 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 49

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to group array of objects ? 5 60
Set up Wildfly 10 8 320
AlarmClock Challenge 35 145
main method of string class 3 84
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
A Short Story about the Best File Recovery Software – Acronis True Image 2017
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now