3

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 ?

2
  • This might help you: stackoverflow.com/a/24343067/259889 Commented Jan 31, 2015 at 16:55
  • @Sid this isn't very helpful for me ... Commented Jan 31, 2015 at 17:38

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

7 Comments

and vise versa ? i.e. to getAttribute from WebRequest object when it is set using HttpServletRequest.setAttribute();. Please update your answer accordingly too. Thanks
I want to say that is it right that I do like HttpServletRequest.getSession().setAttribute("abc","abcdef");. So can I retreive the session attribute like WebRequest().getAttribute("abc");. I think now its little more clear. Thanks
There is no method getAttribute() taking only a String as argument in WebRequest. Read the API doc.
There is a method named getAttribute() ... It is inherited from org.springframework.web.context.request.RequestAttributes.
|
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.