Joe
Joe

Reputation: 2633

how to get session attribute data members from different servlets?

I have set session attribute like this:

request.getSession().setAttribute("newEmployee", retEmp);

now this attribue is an abject of type Employee that has data member "id", I want to get this from another servlet, so im trying to do something like this:

request.getSession().getAttribute("newEmployee.id").toString()

isn't it suppose to work? tnx

Upvotes: 0

Views: 1529

Answers (2)

Robert Moskal
Robert Moskal

Reputation: 22553

You need to retrieve the value by the same key you put it in under. Then you need to cast it to the sort of object you stored on the session. Once you do, you can manipulate it as you please.

Employee e = (Employee) request.getSession().getAttribute("newEmployee");
String id = e.id;

That's the way we do thing in a strongly typed language like Java (excecpt for the hashing part which would work the same most anywhere).

Upvotes: 1

user5835329
user5835329

Reputation:

You only need

String empId = ((Employee) request.getSession().getAttribute("newEmployee")).getId();

Upvotes: 0

Related Questions