This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Session tracking in servlets  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have very simple - probably silly too - question.
I am trying to add session variable in my application for using the userName of the logged in user.
I know if I were to use it in jsp - I could simply do session.setAttribute("userName", userName);

but here is what I am doing -
Jsp page sends information to a servlet, which queries for that user's info. Based on this I want to fetch the userName from the database and put it in a variable which should be available through out the application for the valid session. How should I be doing that?

If you think there is any other better / alternate way , then pleaase let me know (along with the above question.)
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jill,

You can store the username as a session attribute in the servlet .
 
Greenhorn
Posts: 18
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set session attribute as in code snippet below:

String userName = someDaoClass.getUserName();
...
HttpSession session = request.getSession();
session.setAttribute("userName", userName);

And then get access to variable "userName":
HttpSession session = request.getSession();
String userName = session.getAttribute("userName");

userName attribute will be available unless and until session will be valid.

Is it what you need?
 
jill Na
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah thanks I was not getting the same option on java page for some reason .... but yeah setAttribute worked embarassing ;) though
 
Greenhorn
Posts: 27
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The another way to do it is, store the information in a cookie on client side and grab the value from the cookie whenever it should be shown on frontend.

You can check if the cookie is not present or value is null, grab the value from the database and store it in the cookie.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!