HttpSession getAttribute problems

R

Rob

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
 
S

Sudsy

Rob said:
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" );
 
J

John C. Bollinger

Rob said:
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
(e-mail address removed)
 
R

Rob

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
 
B

Brian A Palmer

Rob said:
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.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top