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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Values in a List form one JSP to Another

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I am creating a list on my first JSP by typing in values, clicking a button an appending the value to a list. I would like to take this list from this JSP and populate a list on a second JSP. The first list establishes a main directory list. The second adds data to each category.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Steve Dyke:
I am creating a list on my first JSP by typing in values, clicking a button an appending the value to a list. I would like to take this list from this JSP and populate a list on a second JSP. The first list establishes a main directory list. The second adds data to each category.



Are you doing this by posting back to the server or with Javascript on the client?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Yes I am posting back to server. I have this issue resolved. Thanks for the help.

I now have another JSP List issue. Again I am building the List on the fly with in the JSP buy user inputing text, clicking a button an adding to list control using JavaScript. When I submit how can I use the list content as an array on my servlet? The items in the list are delimited strings that I will need to parse also example: 861766-401,1,1,45. This is a part number, qty, config, associated seq number.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
java.util.String has a split method that can be used to convert a delimited string to an array.

For pre 1.4 versions of Java, there is the java.util.StringTokenizer class.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I understand how to split a string into an array.

My problem is passing the array from the list box on my JSP to my servlet. The list box contains multiple items:

861766-401,1,1,55
861766-403,1,3,55
861766-405,1,1,55
etc.

Once I get this array into my servlet then I can split each item can't I?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
If something like 861766-401,1,1,55 were the value in the select list options, then sure, you could split it after retrieving it with request.getParameter.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
In my JSP I have a ListBox. How do I pass this List to the Servlet so the Servlet treats it as an Array?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You want to post all of the values in the select list?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Yes, all of the values.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Well, only the selected ones get posted so, you could either select them all using Javascript from the onsubmit event of the form, or, you could store them as hidden input fields in the form and they would all get sent up that way. This would allow you to use the select list to find out which one the user really wanted to select.

Another alternative would be to store them on the server as session variables so that the servlet already has access to them and they don't need to make the round trip to the browser and back.

Without knowing more of your requirement, it's hard to say what the best approach will be for you.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Here goes my best explanation.

The user opens up a form(JSP). It prompts them to supply a list of part numbers that will eventually populate a Buyer Furnished Equipment List(a report that will be displayed in an excel format). On this form there is three input boxes and one listbox and two buttons.

The inputboxes: one for part number, one for configuration, one for quantity.

The Listbox: used for recieving and displaying user input.

The buttons: One button is a general button and is used after the three input boxes are populated to populate the listbox with one item that is a concatination of the values. Then the three input boxes are cleared. The user can type in another set of values, click the button and this set of values are added to the listbox thus creating a list(not a select list in this case but an input array of values).

The second button is a submit button that calls the servlet.

How do I pass the contents of the list that was just created(all of it) to my servlet so the items will be treated as an array of values?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
With HTML forms, if multiple form fields have the same name or in the case of a multiple select list item, the servlet api will present the items to the developer as an array.

Example:


These values can be retrieved with:


Is that enough to get you going?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have this working but if I try to read a value:



I get a NullPointerException error.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Which line is throwing the exception?

Either arg0 hasn't been initialized or getParameterValues is returning null.
The line number will say which is which.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
It is the:

System.out.println(tcl[1]);

that is causing the error. As a matter of fact any line using the tcl will cause an error.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Are you posting an HTML form parameter named 'tclist' to the servlet?
If not, getParameterValues will return null.

 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Yes, the html has parameter names tcList. If I execute the following javascript on my submit I get undefined eventhough there are several items in the list:

var tcList = document.getElementById('tcsal');
alert(tcList.listCount);

My Code for adding items:

tcList.add(new Option(tcinput.value+"("+tcListq.value+","+tcListp.value + "," + holdIndexcontrol.value + ")",tcinput.value));
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Can you post the entire HTML form?
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I think I have figured it out.
1. I had to turn the Allow Multiple Choice on
2. I had to step through the list to select each item

Thanks for all the help. I am sure I will back with another issue shortly.
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Well I thought I had it done until I started working with my AJAX portion of code. How do I use the listbox as a parameter in my url string for the AJAX call?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Steve Dyke:
Well I thought I had it done until I started working with my AJAX portion of code. How do I use the listbox as a parameter in my url string for the AJAX call?



Something like this would be done with Javascript, on the client.

Normally, I'd move the thread to our HTML/Javascript forum but it is already very long and full of non Javascript questions.

Do you mind asking this question down there?
 
Sheriff
Posts: 67746
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:
  • Report post to moderator
Yes, generally "oh, and one more question..." usually means it's time to start a new topic.
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic