ServletRequest.setAttribute question

Discussion in 'Java' started by Joerg Gippert, Jul 30, 2003.

  1. 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
    Joerg Gippert, Jul 30, 2003
    #1
    1. Advertising

  2. Joerg Gippert

    Wendy S Guest

    "Joerg Gippert" <> wrote
    > 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.

    --
    Wendy in Chandler, AZ
    Wendy S, Jul 30, 2003
    #2
    1. Advertising

  3. "Wendy S" <> schrieb im Newsbeitrag
    news:bg9f9t$l8a$...
    > "Joerg Gippert" <> wrote
    > > 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.


    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
    Joerg Gippert, Jul 31, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Santosh
    Replies:
    2
    Views:
    447
    Chris Smith
    Sep 8, 2004
  2. Matt
    Replies:
    7
    Views:
    14,744
    John C. Bollinger
    Nov 8, 2004
  3. Replies:
    1
    Views:
    6,149
    Raymond DeCampo
    Jan 24, 2006
  4. sasuke
    Replies:
    3
    Views:
    1,295
  5. Replies:
    13
    Views:
    1,294
Loading...

Share This Page