• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to use a session variable in a java class?  RSS feed

 
siva prasadjvs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All,

i have a jsp page where i have set the session attribute using session.setAttribute("username",usename);...

and using the request dispatcher i requested the servlet to store the session using

Strings s = (String)session.getAttribute("username");..

please let me know if i want to use the variable stored in s in some other java class(this class will not extends any servlet).

let me know how to do this.

thanks

 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass along as a parameter?
 
siva prasadjvs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please tell an exmple for that?
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
siva prasadjvs wrote:
Strings s = (String)session.getAttribute("username");..
please let me know if i want to use the variable stored in s in some other java class(this class will not extends any servlet).


If you have alread variable "s" with you then you just have to create an object of another java class and pass it into its method.
Is it what you want?
If not then can you please show the code?
 
siva prasadjvs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ishan,

"If you have alread variable "s" with you then you just have to create an object of another java class and pass it into its method."

here is my doubt i did the same by i have taken a POJO class like

"SaveUserSession" and created a variable in my servlet class then i have set my session variable using the following setter method

SaveUserSession s;

s.setUsername(s);"

if i need to get the same variable what i have to do is to instantiate the object of "SaveUserSession "

if once i instantiate the object in another class and call the getter method every time i will get a "null" this is my problem .

please let me know how to solve this.

thanks
siva prasad
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
siva prasadjvs wrote:
if once i instantiate the object in another class and call the getter method every time i will get a "null" this is my problem .
siva prasad


Not a good solution but make userName variable of your POJO as static.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear exactly what the OP is trying to do yet, but storing the value as a static is most certainly not the answer.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:It's not clear exactly what the OP is trying to do yet, but storing the value as a static is most certainly not the answer.


So is it solved by making the class singleton? or by passing the refrence of the object to the other class?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singletons are rarely the right answer either.

Generally, data is passed along to other classes by passing the data.

What makes the most sense, depends what the nature and scope of the data needs to be. And the OP has not done a good job of describing the nature of the data yet.

And by the way, passing around servlet resources like the session isn't usually a good approach either. It violates Separation of Concerns. If a method needs data from the session, the data should be passed, not the session itself.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i agree to your last post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!