aspose file tools*
The moose likes Servlets and the fly likes getting multiple values from select as HttpServletRequest parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting multiple values from select as HttpServletRequest parameter" Watch "getting multiple values from select as HttpServletRequest parameter" New topic
Author

getting multiple values from select as HttpServletRequest parameter

Chris Victor
Greenhorn

Joined: Jun 24, 2002
Posts: 7
I have a servlet with for e.g. HTML output:
<select name=del_cause size=5 multiple>
<option value=1>test
<option value=1>test2
<option value=1>test3
</select>
If I selected multiple values from this select object how can I pass these values (from the document.form1.del_cause object) to my controller servlet?
grtz,
Chris
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
dRanger73
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!

For your question how you pass them is done by the browser when the form is submited. I suspect that what you are really looking for is how to use them in the servlet, right? What your goiong to need to use is the getParameterValues(String name) method. It takes a String as an argument that is th name of the form element, in your case it would be 'del_cause'. The method returns a String array of all of the values sent, or null if parameter doesn�t exist.
The getParameterValues method is a method of the Request class so call it on the request object.
hope that helps.


Dave
Chris Victor
Greenhorn

Joined: Jun 24, 2002
Posts: 7
I tried using the getParameterValues method...but unluckily it returns a null....
Perhaps, it is the fault of Tomcat 4.01 ???
When I do a System.out.println of all Parameters....it does not display the del_cause parameter
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Chris
Thanks for changiong your name, however we ask that you use two names (first and last), thanks.
As for not getting a value for that parameter, I assume you are making a selection on the html page then submitting it. What submit method are you using, get or post? And are you using the appropriate cooresponding method in your servlet, doGet or doPost?
Can you post your code for both the HTML and the code in the servlet that handles it?
Chris Victor
Greenhorn

Joined: Jun 24, 2002
Posts: 7
Hey Dave,
Thanks for taking the trouble to look at my trouble
Anyway I tried using both methods(doGet/doPost) and checked the html very clearly..but the parameter simply doesn not get parsed..
As the code is very substantial..it is not useable on this site..I can mail it to you though if you like...
grtz
Chris
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61772
    
  67

Or better yet, pare down your code to an example that just does the select submission and see if that works as you expect. If not, your code should be small enough to post here. If it works outside of your larger environment, it's a clue that something else you are doing is interfering with the "normal" operation.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Chris, like Bear said if your code is that substantial then try to factor out everything except what you are trying to do then test just that part of it.
If it doesn't work feel free to email it me and I'll take a look at it. thanks for asking by the way, a lot of people send code unsolicited and that I just ignore
look forward to seeing it.
Chris Victor
Greenhorn

Joined: Jun 24, 2002
Posts: 7
ow ow my....It appears I was testing wrong.This is what I did for testing...
Enumeration paramNames = req.getParameterNames();
while(paramNames.hasMoreElements())
{
String paramName = (String)paramNames.nextElement();
System.out.println("ParameterName: "+paramName);
}
String[] params = req.getParameterValues("delcause");
if(params != null)
{
System.out.println("The following selection(s) were made: ");
for(int i = 0; i < params.length; i++)
{
System.out.println(params[i]);
}
}
It appeared that I didn't select Anything, thus the parameter was not filled and given to the Servlet.
Conclusion: The select Parameter will only be passed to the HttpServletResponse.getParameter..if it contains values...
Thanks a lot for your troubles guys...
Chris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting multiple values from select as HttpServletRequest parameter