This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Passing Checkbox values to a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing Checkbox values to a servlet" Watch "Passing Checkbox values to a servlet" New topic
Author

Passing Checkbox values to a servlet

Chris Whited
Greenhorn

Joined: Oct 19, 2012
Posts: 24

I have a JSP that displays a table that is created by SQL Queries. At the end of each row I included a column that contains check boxes. Because the data in the table is constantly changing I need the value of that check box to be changing with it. So in my JSP I used an EL expression that dynamically generates the value of the value field and set it to the primary key field of the database it is querying.
Here is my JSP code that I was mentioning:

*That is just the relevant portion of it.
I know that this works (i.e. it makes each value of the value attribute equal to the primary key field (Ref_Num)). But how do I pass this value to a servlet so it can update the database?

Here is my servlet code:

*again just the relevant portion

When I run this I get a Null Pointer Exception.

Any help would be greatly appreciated. I can include the rest of the code if need be.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

For each value that is checked, you will get an entry in the value array. Unchecked checkboxes will not be included.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chris Whited
Greenhorn

Joined: Oct 19, 2012
Posts: 24

So then is that code correct?

And is my array "completed" being populated with the value attribute values?

And if so, why would I be getting a Null Pointer Exception? Or is
if("ON".equals(completed))
not the correct way of checking whether that check box was checked?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

It is not. The values in the "completed" array will be the values assigned to the checked checkboxes.

Use the Chrome/Safari debugger and look at the request. You should see exactly what is being passed as request parameters.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Chris Whited wrote:So then is that code correct?


Obviously not, since it throws a NullPointerException.

So the first thing for you to do is to find out what line of code is throwing the exception. You have a stack trace, right? It should tell you line numbers. So start there.
Chris Whited
Greenhorn

Joined: Oct 19, 2012
Posts: 24

Okay I changed my code to:


To check and see if the values were even being passed to it. And in my console this is what I was getting:
1.0
2.0
Database was updated (the 2 values I passed to it were 1.0 and 2.0. So it is getting the values and that is apparently the correct code for that.

But when I put the query and statement code back in (changed from a Statement to a PreparedStatement) I get this error:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

Can I not pass the value of an array as part of an SQL Statement?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

There's nothing in the code you posted which uses SQL statements in any way, so it's impossible to understand what you are asking.
Chris Whited
Greenhorn

Joined: Oct 19, 2012
Posts: 24

The SQL statements were commented out. But never mind. I figured it out. I was incorrectly putting my array into the where clause of the update query.

Here is my code that works and updates the database for the row where the check box is selected:


Thanks everyone for their help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You're only expecting one of the boxes to be checked? Because that code will only work when exactly one of them is checked. Won't work if none are checked, won't work if two or more are checked.

I'd also suggest using a PreparedStatement in the way it was supposed to be used. Here's a fragment of code which shows the general idea:



Chris Whited
Greenhorn

Joined: Oct 19, 2012
Posts: 24

Thank you for the suggestion. I changed the code and moved the PreparedStatement command to the correct spot so that more then one check box can be checked at one time.

Could you divulge on what you mean by this:
I'd also suggest using a PreparedStatement in the way it was supposed to be used. Here's a fragment of code which shows the general idea:

view plaincopy to clipboardprint?
String query = "UPDATE Objectives"
+ " SET Objectives.Completed=True"
+ " WHERE Objectives.Ref_Num = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, completed[i]);


I went to the API and I saw what setString did, but executeUpdate is also mentioned in there. And since I want to update the database, why would I want to do setString.
I am reasonably new to programming so I am still learning a lot of this stuff.

Again, I really appreciate all of the help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Chris Whited wrote:I went to the API and I saw what setString did, but executeUpdate is also mentioned in there. And since I want to update the database, why would I want to do setString.


Here's a tip: the API is a useful tool if you want to know what a method does, but it isn't a useful tool if you want to know why to use a method. For that you should be reading a tutorial. In general if you want to find a tutorial about Java feature X, your web search keywords are "java X tutorial".

In this case the PreparedStatement tutorial is here: Using Prepared Statements.
Chris Whited
Greenhorn

Joined: Oct 19, 2012
Posts: 24

Oh okay. That makes a lot more sense. Thank you very much Paul.
 
jQuery in Action, 2nd edition
 
subject: Passing Checkbox values to a servlet
 
Similar Threads
how to iterate (use another c:foreach or similar ) inside c:foreach in spring
Extracting a parameter from c:out value
Dynamically created Check Boxes in a JSP
JSP column name hyperlink help
Passing row id from html table on jsp to Action Class in Struts