ServletRequest.setAttribute question

J

Joerg Gippert

Hi everyone!

I have an action servlet, that takes an attribute out of the request,
changes it and puts it back into the request. Now I´m wonderring, if I have
to remove the old attribute first, using removeAttribute() or if the API
simply overwrites any existing attribute if the same name is used.

E.g
Myobject o = (Myobject)request.getSession().getAttribute("MYATTRIBUTE")
o.changevlaue = "bla";
request.getSession().setAttribute("MYATTRIBUTE");

or do I have to do a request.getSession().removeAttribute("MYATTRIBUTE")
before I set the new one?

Thanks for any hint!

Regards,
Joerg
 
W

Wendy S

Joerg Gippert said:
I have an action servlet, that takes an attribute out of the request,
changes it and puts it back into the request. Now I´m wonderring, if I have
to remove the old attribute first, using removeAttribute() or if the API
simply overwrites any existing attribute if the same name is used.

You don't have to remove first. It's most likely a Map of some kind holding
the request attributes, so a 'put' using an existing key will replace the
old reference with the new one.
 
J

Joerg Gippert

Wendy S said:
You don't have to remove first. It's most likely a Map of some kind holding
the request attributes, so a 'put' using an existing key will replace the
old reference with the new one.

Thanks for your reply. Should have tried it out myselfe before I post here.
It´s late here already ;)
Yes, a Map came across my mind as well. Just was a bit confused about the
wording in the API (getAttribute) where it says"Returns the value of the
named attribute as an Object" and I was afraid, that maybe two objects of
the same name (old and new one) exist somewhere after I put it back into the
session.

Thanks again for the quick answer!

Regards,
Joerg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top