aspose file tools*
The moose likes JSP and the fly likes Pass some checkbox values from one JSP to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Pass some checkbox values from one JSP to another" Watch "Pass some checkbox values from one JSP to another" New topic
Author

Pass some checkbox values from one JSP to another

Pablo Zuazua
Greenhorn

Joined: Feb 07, 2012
Posts: 6
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pablo Zuazua
Greenhorn

Joined: Feb 07, 2012
Posts: 6
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

Joined: Feb 07, 2012
Posts: 6
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

Any modern book on JSP should cover the JSTL and EL.
Pablo Zuazua
Greenhorn

Joined: Feb 07, 2012
Posts: 6

Ok. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass some checkbox values from one JSP to another