• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass some checkbox values from one JSP to another

 
Pablo Zuazua
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been having problems passing parameters from one .jsp to another.

I have two .jsp (1 and 2). In 1 I get some data from a database and show the user a bunch of checkboxes (depending on the data I got before). The user has to check one or more of the checkboxes, the selected will be deleted in my database in 2. (It´s something like "Select the numbers you want to delete").

I don't know how to pass the selected checkboxes and the value from 1 to 2. I tried with javascript/jQuery, trying to know if a checkbox is checked and its value, add the value to a hidden field and use the request in 2 to get it.
1.jsp

Javascript/jQuery

2.jsp (here I only try to know if I have the info)


When I try to access to request.getParameter("amount") I get an java.lang.NumberFormatException: null so I think my Javascript/jQuery is wrong.

How can I solve this?.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, the obligatory: what are you doing putting Java code into a JSP in 2012? Scriptlets have been discredited since 2002. Time to update your JSP knowledge with the JSTL and EL.

When you submit the form, the checkbox value will be sent if the control is checked. Otherwise, nothing will be sent.

The getParameter() call will never throw a NumberFormatException. It's some other code that's doing that. Perhaps some following code that can't deal with null properly.
 
Pablo Zuazua
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:First of all, the obligatory: what are you doing putting Java code into a JSP in 2012? Scriptlets have been discredited since 2002. Time to update your JSP knowledge with the JSTL and EL.


Ok, I should update myself I did it in that way because I thought it was something quick and do not give me so many problems.

Bear Bibeault wrote:When you submit the form, the checkbox value will be sent if the control is checked. Otherwise, nothing will be sent.


I understand that but how I know what data was sent?.
I mean in a regular form with an input with the atribute id or name set I only have to use the request to get the value:



But in this case I don't know how many checkboxes I will have and even their names or ids to use getParameter.

Bear Bibeault wrote:The getParameter() call will never throw a NumberFormatException. It's some other code that's doing that. Perhaps some following code that can't deal with null properly.


I didn't know that but there´s no more code in the second jsp than the one I've posted I´m my first post and the exception points me to that line.
 
Pablo Zuazua
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I finally found the solution and I don't need any Javascript. Searching in the API I found a method of the request called getParameterNames() that returns an Enumeration of String objects containing the names of the parameters contained in this request.

As Bear said:
Bear Bibeault wrote:When you submit the form, the checkbox value will be sent if the control is checked. Otherwise, nothing will be sent.

So with this information and the method, I changed the way the checkboxes were constructed and use the enumeration to get the values.



So in my second .jsp I get a Enumeration with the names of the selected checkboxes, which are the phone numbers.

Bear thanks for your help, without that clue problably it would have taken me more time.
By the way do you know a good tutorial to learn about JSTL and EL?.


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any modern book on JSP should cover the JSTL and EL.
 
Pablo Zuazua
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic