Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP server side validation for checkbox and radio buttons?  RSS feed

 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I can't figure out how to validate checkbox and radio buttons input types on the server side..
Here's my code:


Form:

Server side:


Can somebody help me? I'm getting frustrated...
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you need to validate about them?
Is this checking for required fields or do the values matter as well?

The first recommendation would be to stop writing java code like that inside a JSP.
Put the handling logic into a servlet, or use a framework to do the heavy lifting for you :-)


What values do you get when you try this out?
What do you want to be the outcome?
What does it do that you don't expect?


EDIT: Oh, and by the way, welcome to the ranch!
Just for future reference when making a post it helps to use Code Tags
There is a handy button for it on the interface. Just select the text you want to be code, and hit the button. I've done it for you this time :-)

 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:What do you need to validate about them?


Thank you for replying and the tips :-)
I need to validate if at least of the checkbox and radio buttons are checked....
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Struts? You should be using the validation framework.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I can't figure out how to validate checkbox and radio buttons input types on the server side..
Here's my code:


Form:

Server side:


The user must at least check one on checkboxes..
Can somebody help me? I'm getting frustrated...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, the Java code should be in a servlet, not a JSP. JSP is for generating views, not processing data or anything else. Putting Java code in a JSP is a bad practice that's been obsolete for 15 years. 15 years!

Secondly, if you want to make sure that at least one checkbox is returned, why not just check the array length for zero?
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Firstly, the Java code should be in a servlet, not a JSP. JSP is for generating views, not processing data or anything else. Putting Java code in a JSP is a bad practice that's been obsolete for 15 years. 15 years!

Secondly, if you want to make sure that at least one checkbox is returned, why not just check the array length for zero?


Well.. that's how I was taught by the teacher.. although he mentioned servlets, we never really had any serious discussion about it.
Thank you for your reply..
So you mean i should learn servlets?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't tell us this was homework. Please be sure to do so in the future.

If your instructor hasn't taught servlets yet, perhaps you should seek his guidance. Just be aware that in the professional world, putting Java code in a JSP is not acceptable.
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You didn't tell us this was homework. Please be sure to do so in the future.

If your instructor hasn't taught servlets yet, perhaps you should seek his guidance. Just be aware that in the professional world, putting Java code in a JSP is not acceptable.


Thank you for these informations. I can't believe he taught us poorly..
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:mammshean santiez,
I have merged your topic into this topic. I hope that helps.


EDIT: Please don't ask the same question again on another forum here.

My questions from before still stand:

What values do you get when you try this out?
What do you want to be the outcome?
What does it do that you don't expect?


It's very fine. Thank you.. I'm new to coderanch and thanks for helping me.

To answer your questions..
Whenever i leave all of the checkbox unchecked it gives me error, if i check at least one it doesnt.
I just wanted to validate if every checkbox is unchecked then i would want to show an error message.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mammshean santiez wrote:
I just wanted to validate if every checkbox is unchecked then i would want to show an error message.

You don't need to check every one. In fact, you can't -- an unchecked checkbox does not participate in the submission.

I already told you how to test if none are checked.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Whenever i leave all of the checkbox unchecked it gives me error, if i check at least one it doesnt.

More information please.
Is there an error message? What does it say?
Is there anything that you can think of that would resolve the error message?

Have you seen Bear's response?
>Secondly, if you want to make sure that at least one checkbox is returned, why not just check the array length for zero?

Have you tried it?
Does it work?
If not, what happens? Error messages?


What might help with resolving your issue is if you understand what is happening with the underlying HTML controls.
When you submit a form all the controls in that form will send request parameters to the server as name=value.
Checkboxes - will send their name=value pairs in the request when they are selected, or nothing if they are not.
Radio button - will send one name=value pair in the request when one is selected, or nothing if one is not.

Your code looks like it handles Radio buttons.
It doesn't look like you have the checkbox handling quite right.

The answer should be available in the API for javax.servlet.ServletRequest:

java.lang.String[] getParameterValues(java.lang.String name)
Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
If the parameter has a single value, the array has a length of 1.


 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mammshean santiez wrote:

Thank you for these informations. I can't believe he taught us poorly..


It's not uncommon, sadly, so I'm not sure how much blame can be laid at the teacher, rather than the course material in general.

I have long said that they get this backwards. They should start with servlets, then bolt on JSPs after a functioning "Hello World" has been written. I do wonder if it's some PHPish throwback that results in JSPs being taught first, and then they never update their course notes.

...sorry for the rant...
:)
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
mammshean santiez wrote:

Thank you for these informations. I can't believe he taught us poorly..


It's not uncommon, sadly, so I'm not sure how much blame can be laid at the teacher, rather than the course material in general.

I have long said that they get this backwards. They should start with servlets, then bolt on JSPs after a functioning "Hello World" has been written. I do wonder if it's some PHPish throwback that results in JSPs being taught first, and then they never update their course notes.

...sorry for the rant...


So i should be better off learning servlets?
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:>Whenever i leave all of the checkbox unchecked it gives me error, if i check at least one it doesnt.

More information please.
Is there an error message? What does it say?
Is there anything that you can think of that would resolve the error message?

Have you seen Bear's response?
>Secondly, if you want to make sure that at least one checkbox is returned, why not just check the array length for zero?

Have you tried it?
Does it work?
If not, what happens? Error messages?


What might help with resolving your issue is if you understand what is happening with the underlying HTML controls.
When you submit a form all the controls in that form will send request parameters to the server as name=value.
Checkboxes - will send their name=value pairs in the request when they are selected, or nothing if they are not.
Radio button - will send one name=value pair in the request when one is selected, or nothing if one is not.

Your code looks like it handles Radio buttons.
It doesn't look like you have the checkbox handling quite right.

The answer should be available in the API for javax.servlet.ServletRequest:

java.lang.String[] getParameterValues(java.lang.String name)
Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
If the parameter has a single value, the array has a length of 1.





I have tried Bears' response and it doesnt work. But I have solved the issue by simply checking if the array is null or not.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to hear you got it working.

As you are hinting, the initial error you were getting from your code would have been a null pointer exception from trying to access the array of checkbox results when no checkboxes were ticked.
There should have been a stacktrace pointing you at which line was going wrong.

 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Glad to hear you got it working.

As you are hinting, the initial error you were getting from your code would have been a null pointer exception from trying to access the array of checkbox results when no checkboxes were ticked.
There should have been a stacktrace pointing you at which line was going wrong.



Thank you for helping and for trying to..
and yes it was giving me a null pointer exception error

Thank y'all
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mammshean santiez wrote:

So i should be better off learning servlets?


You're best off doing whatever is needed to get you through the subject, first, sadly.

But yes, in terms of actually learning how a proper Java web app fits together you are best off (IMO) starting with a servlet, then having it forward to a JSP to display something.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Proper structure of professional Java web applications is described in this article.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!