Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Any faults in this implementation of sessions??  RSS feed

 
varun bihani
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actuaaly I am not sure whether I am using sessions in my site correctly.
Wat I am doing is like this...
Whenever user logs in my site with username password I set his session as--
session.setAttribute(sessionname,anyvariable) here anyvariable is usually userid...
and in subsequent pages I extract that session variable as--
String session_name=session.getAttribute(sessionname) and this session_name is that anyvariable that I have set.
And if session_name is not null and session_name is equaly to that userid(in my case) then his session is true...
Well I just want to know does this implementation has any bugs???
 
jyothsna kumari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai varun,
you can do that.but the thing this since session.getAttribute()returns an object you need to cast it into a string something like this
String session_name=(String)session.getAttribute(sessionname);
and rest of it if fine.
thanks,
jyothsna
 
S. Ganapathy
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Varun,
When the client sends a request first time to the server, it creates and attaches a session to your request with the session id. Each session is identified with the session id. Server maintains each client's session in the form of name-value pairs(Here it is sessionID-SessionObject).
When you call request.getSession(), you will be receiving session object bound to your request by the server. Each time client sends a request to server, session id is also attached to request by cookies, or url rewriting, or in the form of hidden form fields, etc.
You are actually adding attributes to your session. This is possible with session.setAttribute(key, value). As long as your session is alive, you can get the previously set attributes using session.getAttribute(key).
Hope you understood about the session.
Regards,
Ganapathy,S
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!