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
|
|
0
|
|
|
|
Reply
|
rob516 (14)
|
9/8/2003 4:15:44 AM |
|
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" );
|
|
0
|
|
|
|
Reply
|
bitbucket44 (1434)
|
9/8/2003 10:40:26 AM
|
|
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
jobollin@indiana.edu
|
|
0
|
|
|
|
Reply
|
jobollin (1553)
|
9/9/2003 3:09:56 PM
|
|
On Tue, 09 Sep 2003 10:09:56 -0500, "John C. Bollinger"
<jobollin@indiana.edu> 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
|
|
0
|
|
|
|
Reply
|
rob516 (14)
|
9/12/2003 12:29:04 AM
|
|
Rob <rob@windowsbox.box> 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
|
|
0
|
|
|
|
Reply
|
bpalmer (77)
|
9/14/2003 1:24:48 AM
|
|
|
4 Replies
47 Views
(page loaded in 0.115 seconds)
|