Stefan Evans wrote:What do you need to validate about them?
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?
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.
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?
mammshean santiez wrote:
I just wanted to validate if every checkbox is unchecked then i would want to show an error message.
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.
mammshean santiez wrote:
Thank you for these informations. I can't believe he taught us poorly..
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...
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.
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.
mammshean santiez wrote:
So i should be better off learning servlets?
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |