Link to home
Start Free TrialLog in
Avatar of sitijaafar
sitijaafarFlag for Malaysia

asked on

Get list box value in servlet

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

Avatar of Farzad Akbarnejad
Farzad Akbarnejad
Flag of Iran, Islamic Republic of image

Hello,
Use <%= request.getParameter("value") %> not getParameterValues method.

-FA
Avatar of sitijaafar

ASKER

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
<< but cannot work,>>
what does this mean? can you share the result of
System.out.println(request.getParameter("value"));
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

Use <%= request.getParameter("learnCountry") %>

-FA
It didn't get the value of the country
<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..
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..

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.




Yes, it's multiple list box, so can select multiple values.
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.
In servlet  are you getting other values correctly?
May be you are not submitting form correctly in JSP.

ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good