aspose file tools*
The moose likes Servlets and the fly likes Using Values in a List form one JSP to Another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Using Values in a List form one JSP to Another

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Nov 16, 2004
Posts: 1490
    
    1
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Nov 16, 2004
Posts: 1490
    
    1
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

Joined: Dec 11, 2004
Posts: 13410

You want to post all of the values in the select list?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
Yes, all of the values.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Nov 16, 2004
Posts: 1490
    
    1
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Nov 16, 2004
Posts: 1490
    
    1
I have this working but if I try to read a value:



I get a NullPointerException error.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Nov 16, 2004
Posts: 1490
    
    1
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

Joined: Dec 11, 2004
Posts: 13410

Are you posting an HTML form parameter named 'tclist' to the servlet?
If not, getParameterValues will return null.

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
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

Joined: Dec 11, 2004
Posts: 13410

Can you post the entire HTML form?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1490
    
    1
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

Joined: Nov 16, 2004
Posts: 1490
    
    1
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

Joined: Dec 11, 2004
Posts: 13410

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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

Yes, generally "oh, and one more question..." usually means it's time to start a new topic.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Values in a List form one JSP to Another