Junaid S.
Junaid S.

Reputation: 2642

Get and set attributes in session - Spring framework

I am using Spring Framework. Well in some part of program I store an Attribute in session like this

req.setAttribute("learnerGroup", form.getGroup(), WebRequest.SCOPE_SESSION); // save the object/String in session 

Where req is of type - WebRequest.

In the other part of program I have HttpServletRequest hreq.

Questions

1) Is it right way to save Objects is session, like I did above using WebRequest ?

2) How do I retrieve the saved Object from the session using HttpServletRequest ?

Upvotes: 3

Views: 3488

Answers (2)

Himadri Mandal
Himadri Mandal

Reputation: 335

session object attributes are final, so you may not be able to update them. But you can try this: perform getAttribute(), save it somewhere and setAttribute().

Sessions are not subjected to change or update, they are meant to be created and invalidated. Maybe that is the reason, we do not have update Attribute there.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691635

  1. Yes, that will indeed save the object in the session attribute named "learnerGroup".

  2. using request.getSession().getAttribute("learnerGroup")

Upvotes: 4

Related Questions