• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get the username from session?  RSS feed

 
sabin ran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i created the http session after the username and password validation. now i want to use that username to be used in the database query to fence the data from table with that username.
i have been able to display the username in the jsp page. thanks
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you need to set the username in the session object as attribute after successful validation of user.

Then you can retrieve it using getAttribute.

Check this article for more details.
 
sabin ran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for reply. i have already been able to display the username in the jsp page as you have mention and the article to suggested. but the problem is little different. i have created the session during the authentication. the user gets into his home page, navigate to different jsp page(x) with link and in that X page i want to show the data fence from the database according to the username of that session.
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a session is created for the user, are you storing username in the session ?

One the username is stored in the session as attribute, you can get the username from the session in any jsp/servlet, unless the session is invalid.




 
sabin ran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.. i am storing the username in the session with

session.setAttribute("user", name) in the Authenticate.java- servlet

but i am not being able to get that username in the UserStatus.java- servlet. i have tried

String username= (String)session.getAttribute("user"); but its does not work.

what have i missed out here? please help.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Click here

Me to studying servlets still stuck in response ....hope that the above link helps you out
 
Chinna Eranna
Ranch Hand
Posts: 174
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print Session Id in following scenarios:
just after setting the attribute in Authenticate.java servlet.
just before retrieving the attribute in UserStatus.java servlet.

Session Id can be retrieved by using session.getId().

You must see the same Id.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!