Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Checking user selection

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an html listbox with options available to the user along with an order button and a reset button.


I have my servlet setup now so that I can get 1 or more options and display to order with the correct total.


what I'm trying to do is, if the user hits the order button and has not made any selection, I want to keep them on the index.html page and have a message how up asking them to make a selection.

I've tried different things along these lines


but that's taking me to the servlet page and not printing anything... can someone steer me in the right direction?
also, if you see other things in there that can be better in terms of programming practice, please let me know!

many thanks!
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this something you want to do on the client with JavaScript, or wait until the form is submitted and check it n the server? (or both?)
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Is this something you want to do on the client with JavaScript, or wait until the form is submitted and check it n the server? (or both?)



In this case, wait until the form is submitted, then run the check.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so perform the check within the servlet code, and if it fails, redirect back to the page with information that the validation failed. The session would be a good place to store message info (be sure to clean it out when done) if you'd rather not tack it onto the URL itself.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. Is there a good reason to generate the HTMl in the servlet rather than forwarding to a JSP to create the HTML?
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:OK, so perform the check within the servlet code, and if it fails, redirect back to the page with information that the validation failed. The session would be a good place to store message info (be sure to clean it out when done) if you'd rather not tack it onto the URL itself.



This is what I'm trying to do, but I'm failling on getting the right test with the listbox. I thought that using the

where items is the array that is using the getParameterValues() to get the selected ones. So I figured that when it's 0 (none selected), this would work, but it isn't.... is that the correct way to test a that a listbox has been selected?

With regards to your ps, I do not know as I am only in my 3rd week of learning servlets, so I think JSPs are coming soon.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly are you getting back as results if not an empty array?
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john krem wrote:With regards to your ps, I do not know as I am only in my 3rd week of learning servlets, so I think JSPs are coming soon.


Ah, OK. You will discover (hopefully) that emitting HTML from a servlet is an anti-pattern to be generally avoided. But as you are just learning for now...
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:What exactly are you getting back as results if not an empty array?



well when the user selects an item from the listbox, i'm getting back an array of 1 or more values. I validated this with a check of checking the length of the array with an


and when I had 1 item selected, got back 1, with 2 got back 2 etc... but when I made no selection, that check wouldn't even print...
My thinking is that if I check the length of the array, and it's 0, then I want to redirect back the the index.html page with a message... ?
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

john krem wrote:With regards to your ps, I do not know as I am only in my 3rd week of learning servlets, so I think JSPs are coming soon.


Ah, OK. You will discover (hopefully) that emitting HTML from a servlet is an anti-pattern to be generally avoided. But as you are just learning for now...



interesting.... this is good to know, as there seems to be a lot of things we are learning that are questionable... will keep this in my back pocket as we get into JSPs.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see where your check is happening in the grands scheme of things. Is there some conditional that's preventing the check from happening under certain circumstances?
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:I don't see where your check is happening in the grands scheme of things. Is there some conditional that's preventing the check from happening under certain circumstances?


i tried it in my for loop and above the for loop. also, when you asked what i was returning? did you mean it's returning as a null?
just tried this too:


again, within my for loop.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not show the code at this point?

Also, for debugging purposes, I'd recommend printing ut the length of the array rather than doing an "if". That way, you don;t have to try and guess whether the code was executed or not.
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Why not show the code at this point?

Also, for debugging purposes, I'd recommend printing ut the length of the array rather than doing an "if". That way, you don;t have to try and guess whether the code was executed or not.





here are the 2 different approaches I've been playing around with. I have been printing out the items.length too and it's working perfectly when I select 1 or more items, it's just when I select no items that just doesn't want to work for me.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: use proper indentation. It'll make you code easier to read.

Change the debug statement to print the length without any conditional. What does it print?
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Hint: use proper indentation. It'll make you code easier to read.

Change the debug statement to print the length without any conditional. What does it print?



I'm not sure what you mean by withouy any conditional, but, I just did some testing in a new file, and my test works in terms of


so I'm not sure why it's not working in the servlet. I have everything in the processRequest() in the servlet, could that have anything to do with it?
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john krem wrote:I'm not sure what you mean by withouy any conditional

Just print out the length of the array withut testing it. What does it say?

so I'm not sure why it's not working in the servlet.

This seems to imply that it is working elsewhere?

I have everything in the processRequest() in the servlet, could that have anything to do with it?

processRequest() is not a standard method of a servlet so I'd have no way of knowing. I assume it's a method you are calling from one of doPost() or doGet()?

If so, it depends how it's being called and what you are passing to it.

Flying blind here without any code to go on...
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the whole class


What happens is when I have no values selected, the servlet page loads, but the only thing that prints is :


It works in an entirely new project, checking the array.length==0... it's just in this that for whatever reason it isn't, or at least doesn't seem to be.

 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you checked the logs for exceptions?
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Have you checked the logs for exceptions?



NullPointerException at the


makes sense as there is no value being passed, which is what I want....
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, then you now know that items will be null, rather than an enpty array, when no items are selected. So that is what you'll need to test for,
 
john krem
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:OK, then you now know that items will be null, rather than an enpty array, when no items are selected. So that is what you'll need to test for,



tried that too, but still ran into the same issue. the print statement within that test block doesn't print.

i'm going to call it a day with this for now, but i really appreciate your help.

well, i didn't step away and got it going! thanks again!
 
reply
    Bookmark Topic Watch Topic
  • New Topic