Solved

Get list box value in servlet

Posted on 2010-09-20
15
1,841 Views
Last Modified: 2012-05-10
Hi experts, I'm having a problem in getting a value of list box in servlet.  For get the value I already used this code : req.getParameterValues("learnCountry")

below is the list box code:
<select id='learnCountry' name='learnCountry' size="6" style="width:200px" <%=disabledLearnMode%>>
						  <%
							String [] country2= country.split(",");
							for(int h=0; h<country2.length; h++) {
							 String select=country2[h];%>
							<option value='<%=country2[h]%>'> <%=select%> </option>	
							<%}%>							
							
						 </select>	

Open in new window

0
Comment
Question by:sitijaafar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 33714887
Hello,
Use <%= request.getParameter("value") %> not getParameterValues method.

-FA
0
 

Author Comment

by:sitijaafar
ID: 33714917
I already use request.getParameter("value")  but cannot work, this because in the list box have a multiple value because after I add the list of option I want to get all the values:
For example:
         UK
         US
         New Zealend
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
LVL 40

Expert Comment

by:gurvinder372
ID: 33714928
<< but cannot work,>>
what does this mean? can you share the result of
System.out.println(request.getParameter("value"));
0
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 33714947
Hello,
Actually...  with a MULTIPLE select box, you will need to use a
StringTokenizer, since the parameter value will be a comma separated
list of all select options...



Hope that helps!

for (Enumeration p = request.getParameterNames() ; p.hasMoreElements();) {
   name = p.nextElement().toString() ;
   value = req.getParameter(name) ;
   if(name.equals("NAME")) {
      st = new StringTokenizer(value, ",");
      while (st.hasMoreTokens()) {
         // do something with each token.
         System.err.println(st.nextToken());
      }
   }
}

Open in new window

0
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 33714958
Use <%= request.getParameter("learnCountry") %>

-FA
0
 

Author Comment

by:sitijaafar
ID: 33714966
It didn't get the value of the country
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 33715006
<select id='learnCountry' name='learnCountry' size="6" style="width:200px" <%=disabledLearnMode%>>

i think  the select box is disabled in your code then value will not come in servlet.

try once without  disabling the select box then value will come in servlet..
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 33715214
don't use <%=disabledLearnMode%> in select tag ;i think you are disabling the select box.

try once without  disabling the select box then value will come in servlet..

0
 
LVL 11

Expert Comment

by:Manish
ID: 33715576
First , is it multiple select box?  Add multiple
<select multiple id='learnCountry'
what is output of this  <%=disabledLearnMode%>?
req.getParameterValues is correct way to get multiple selected values.




0
 

Author Comment

by:sitijaafar
ID: 33721800
Yes, it's multiple list box, so can select multiple values.
0
 

Author Comment

by:sitijaafar
ID: 33721859
I already add <select multiple id='learnCountry'

the <%=disabledLearnMode%> is for disabled the list box.  Because I have make an option whether to disable it or not.  In current situuation I'm not disabled it. In the servlet I used req.getParameterValues and the result is null also when used req.getParameter.
0
 
LVL 11

Expert Comment

by:Manish
ID: 33722275
In servlet  are you getting other values correctly?
May be you are not submitting form correctly in JSP.

0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33722307
can you show how you are submitting the values? and also like fellow expert @karanw is suggesting, please check if other values are getting submitted properly

Would suggest that you check the view source from browser and see if the select bpx is disabled or not.

Let me know the result of
System.out.println(request.getParameter("learnCountry"));
0
 

Author Closing Comment

by:sitijaafar
ID: 33926687
Good
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question