Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Session data in servlets

Hi

We are trying to include our user name variable in our chat program as the nickname session variable that was registered when they logged in.

We have included the HttpSession session = req.getSession(true); in the servlet.
In our code we are trying to assign the variable userName as 'session.getAttribute("name")' that was registered when they logged in.

The problem we have is the the session variable seems to be an object and we need to convert it into a string. How can we do this.

Basically what we want it (pseudo code):

String username = session.getAttribute("name");

I know this session variable exists as i have used it in previous JSP pages.

Many thanks

Sambo
[762 byte] By [sboothman] at [2007-11-11 10:09:19]
# 1 Re: Session data in servlets
um...i dont know anything about applets or what your trying to do, but couldnt you just call the .toString() method or type-cast it?

String username = (String)session.getAttribute("name"); //type-casting...may throw an unconvertible error
//or
String username = (session.getAttribute("name")).toString(); //call the .toString() method.
anubis at 2007-11-11 22:31:55 >
# 2 Re: Session data in servlets
cheers that did the job
sboothman at 2007-11-11 22:33:00 >