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
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