HttpSession getAttribute problems

Discussion in 'Java' started by Rob, Sep 8, 2003.

  1. Rob

    Rob Guest

    Hello.
    In a jsp file, I have a number of checkboxes. Each checkbox is
    named "msg" and has a value. Example:

    <form>
    <input type=checkbox name="msg" value="1">
    <input type=checkbox name="msg" value="2">
    <input type=checkbox name="msg" value="3">
    </form>

    I use JavaScript to make sure at least 1 checkbox is selected.
    That's why each checkbox is named the same. Also, the checkboxes
    are created on the fly. I pull info out of a database and create
    a checkbox. The checkbox value is determined by the information
    gathered from the database.

    Now, how do I got about pulling out the value tag from each
    checkbox that is seleced and storing it in an ArrayList ?

    If i try:
    HttpSession session = request.getSession();
    String value = (String)session.getAttribute("msg");

    it will give me the first attribute named "msg"...but there can
    be 2 or 3 attributes with that name, depending on how many were
    selected.

    I thought about using session.getAttributeNames(), but I don't
    think it'll help me. After all, each checkbox has the same name,
    "msg".

    I also thought of naming each checkbox like so:

    <input type=checkbox name="msg1" value="1">
    <input type=checkbox name="msg2" value="2">

    And pulling out each value like so:

    Enumeration enum = session.getAttributeNames();
    String name = null;
    String value = null;
    ArrayList messageList = new ArrayList();
    while(enum.hasMoreElements()){
    name = (String)enum.nextElement();
    if(name.startsWith("msg")){
    value = (String)session.getAttribute(name);
    messageList.add(value);
    }
    }

    value = (String)session.getAttribute(name) seems uncecessary
    as name itself has the value stored in it. I'd just need to use
    substring() to pull the value out.

    Can anyone offer a simpler method?

    -Rob
    Rob, Sep 8, 2003
    #1
    1. Advertising

  2. Rob

    Sudsy Guest

    Rob wrote:
    > Hello.
    > In a jsp file, I have a number of checkboxes. Each checkbox is
    > named "msg" and has a value. Example:
    >
    > <form>
    > <input type=checkbox name="msg" value="1">
    > <input type=checkbox name="msg" value="2">
    > <input type=checkbox name="msg" value="3">
    > </form>
    >
    > I use JavaScript to make sure at least 1 checkbox is selected.
    > That's why each checkbox is named the same. Also, the checkboxes
    > are created on the fly. I pull info out of a database and create
    > a checkbox. The checkbox value is determined by the information
    > gathered from the database.
    >
    > Now, how do I got about pulling out the value tag from each
    > checkbox that is seleced and storing it in an ArrayList ?
    >
    > If i try:
    > HttpSession session = request.getSession();
    > String value = (String)session.getAttribute("msg");


    String values[] = request.getParameterValues( "msg" );
    Sudsy, Sep 8, 2003
    #2
    1. Advertising

  3. Rob wrote:

    > Hello.
    > In a jsp file, I have a number of checkboxes. Each checkbox is
    > named "msg" and has a value. Example:
    >
    > <form>
    > <input type=checkbox name="msg" value="1">
    > <input type=checkbox name="msg" value="2">
    > <input type=checkbox name="msg" value="3">
    > </form>
    >
    > I use JavaScript to make sure at least 1 checkbox is selected.
    > That's why each checkbox is named the same. Also, the checkboxes
    > are created on the fly. I pull info out of a database and create
    > a checkbox. The checkbox value is determined by the information
    > gathered from the database.
    >
    > Now, how do I got about pulling out the value tag from each
    > checkbox that is seleced and storing it in an ArrayList ?
    >
    > If i try:
    > HttpSession session = request.getSession();
    > String value = (String)session.getAttribute("msg");
    >
    > it will give me the first attribute named "msg"...but there can
    > be 2 or 3 attributes with that name, depending on how many were
    > selected.
    >
    > I thought about using session.getAttributeNames(), but I don't
    > think it'll help me. After all, each checkbox has the same name,
    > "msg".
    >
    > I also thought of naming each checkbox like so:
    >
    > <input type=checkbox name="msg1" value="1">
    > <input type=checkbox name="msg2" value="2">
    >
    > And pulling out each value like so:
    >
    > Enumeration enum = session.getAttributeNames();
    > String name = null;
    > String value = null;
    > ArrayList messageList = new ArrayList();
    > while(enum.hasMoreElements()){
    > name = (String)enum.nextElement();
    > if(name.startsWith("msg")){
    > value = (String)session.getAttribute(name);
    > messageList.add(value);
    > }
    > }
    >
    > value = (String)session.getAttribute(name) seems uncecessary
    > as name itself has the value stored in it. I'd just need to use
    > substring() to pull the value out.


    Well, I can offer one that will work. You seem to have at least two
    misconceptions about the servlet API that you will need to get straight.
    First, the form data posted by your client, and/or the query string
    parameters in your client's get or post request are accessed through the
    relevant request object, not through the session. That should seem
    perfectly natural, as they are specific to exactly one request, at least
    from an HTTP perspective. Second, which might become obvious once you
    start looking at the ServletRequest class, you want the "parameters" not
    the "attributes". I think you will find that
    ServletRequest.getParameterValues(String) is very convenient for what
    you want.

    Also, an aside on attributes, since you raised the topic. It is not
    uncommon that people new to the servlet API confuse attributes and
    parameters, or, once they know the difference, fail to appreciate the
    usefulness of request, session, and application attributes. These are
    arbitrary objects bound to the appropriate context by a String key, and
    they can be used for persistent storage with appropriate scope. If you
    do much servlet / JSP programming you will probably end up using them
    frequently. (Among other things, JSP uses attributes behind the scenes
    to bind JavaBeans to the specified scope.)


    John Bollinger
    John C. Bollinger, Sep 9, 2003
    #3
  4. Rob

    Rob Guest

    On Tue, 09 Sep 2003 10:09:56 -0500, "John C. Bollinger"
    <> wrote:


    >Well, I can offer one that will work. You seem to have at least two
    >misconceptions about the servlet API that you will need to get straight.
    > First, the form data posted by your client, and/or the query string
    >parameters in your client's get or post request are accessed through the
    >relevant request object, not through the session. That should seem
    >perfectly natural, as they are specific to exactly one request, at least
    >from an HTTP perspective. Second, which might become obvious once you
    >start looking at the ServletRequest class, you want the "parameters" not
    >the "attributes". I think you will find that
    >ServletRequest.getParameterValues(String) is very convenient for what
    >you want.
    >


    You're right. Requests, responses, sessions is not something I'm
    very comfortable working with. I'm practicing JSP/Servlets by
    developing a guestbook like utility. Users can sign up for the
    service and they're given some pre-fabricated HTML code to paste
    into their webpage. I handle inserting entries. I allow users
    to log in, view their entries, delete unwanted entries, change
    their password and stuff like that. I saw that I needed to carry
    information around, such as username and stuff, so I jumped on
    using a session object to carry all this information around
    while the user was logged in. This is why I was trying to
    extract information from a session object.

    -Rob
    Rob, Sep 12, 2003
    #4
  5. Rob <> writes:

    > Hello.
    > In a jsp file, I have a number of checkboxes. Each checkbox is
    > named "msg" and has a value. Example:
    >
    > <form>
    > <input type=checkbox name="msg" value="1">
    > <input type=checkbox name="msg" value="2">
    > <input type=checkbox name="msg" value="3">
    > </form>
    >
    > I use JavaScript to make sure at least 1 checkbox is selected.
    > That's why each checkbox is named the same. Also, the checkboxes
    > are created on the fly. I pull info out of a database and create
    > a checkbox. The checkbox value is determined by the information
    > gathered from the database.


    Others have pointed you towards getParameterValues(). Just as a
    general advice, using javascript to guarantee that at least one is
    selected provides no guarantee at all. You need to check that sort of
    stuff at the server end, also, or you may end up either allowing data
    into your dataset that violates your invariant, getting a
    NullPointerException somewhere down the line, or something even
    worse.

    --
    See comp.lang.java.announce for java-related announcements
    Brian A Palmer, Sep 14, 2003
    #5
    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. Replies:
    3
    Views:
    2,845
  2. =?Utf-8?B?cHJhZGVlcF9UUA==?=

    HttpSession Class

    =?Utf-8?B?cHJhZGVlcF9UUA==?=, Oct 16, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    7,117
    Joerg Jooss
    Oct 16, 2005
  3. Ranger
    Replies:
    1
    Views:
    483
    John C. Bollinger
    Aug 4, 2003
  4. Steve Claflin
    Replies:
    2
    Views:
    1,637
    Steve Claflin
    Aug 15, 2003
  5. Jim
    Replies:
    3
    Views:
    16,452
Loading...

Share This Page