1

I have a strange issue with dropdown boxes in jsp/servlet. Here it is...

  <select name="locdropdown" onchange="javascript:change()" > 
<%
for(LocationDO locationDO : locationList){%>
<option value=<%=locationDO.getLocationName().trim()%>><%=locationDO.getLocationName().trim()%></option> 
<%} %>
</select>

values displayed are:

 BI Sholingar
 BI Mahindra City
 BI Sanand 
 Rolltec_DTA
 Aztec Auto Ltd
 BI Gurgoan

and here is how I try to read it in servlet.

String locclasses = req.getParameter("locdropdown");
System.out.println(locclasses);

assume I select Aztec Auto Ltd then expected output from servlet is same right. But output is Aztec. similarly, if I select BI Sanand, the actual output that comes is BI

Can someone help please

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
narayanan
  • 515
  • 2
  • 10
  • 19

2 Answers2

3

You need to quote the value.

<option value="<%=locationDO.getLocationName().trim()%>">

The space is namely a HTML attribute separator. A browser with a bit decent syntax highlighter would already have hinted it when you have checked the generated HTML by rightclick page > View Source.

<option value=Aztec Auto Ltd>

versus

<option value="Aztec Auto Ltd">
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    You're welcome. The `onchange="javascript:change()"` in your code makes by the way no utter sense. It's basically calling itself. Remove it. Using *scriptlets* is also [discouraged](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files). I'd suggest to investigate taglibs/EL. – BalusC May 27 '11 at 04:39
  • @mahendraliya: an answer can at earliest be accepted 15 minutes after the question is been posted. – BalusC May 27 '11 at 04:41
  • It was not letting me to accept it within few minutes. Accepted now. Actually javascript:change() is making some work for me. I have not attached the entire jsp. So you may not see the purpose. – narayanan May 27 '11 at 04:48
  • Oh, it's your own function? I'd rename it, e.g. `changeLocation()`, you never know in the webbrowser world. – BalusC May 27 '11 at 04:50
0

As said by BalusC in his answer the problem is with your value assignment.

Modify your code as :

<select name="locdropdown" onchange="javascript:change()" > 
<%
for(LocationDO locationDO : locationList)
{%>
<option value="<%=locationDO.getLocationName().trim()%>" >
        <%=locationDO.getLocationName().trim()%>
</option> 
<%} 

%>
</select>

Hope this helps.

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
  • 2
    That's right. I'm only not sure why you are repeating an already given answer. – BalusC May 27 '11 at 04:44
  • I just posted the complete code, which can be used directly. Its just that you pointed out, but didn't provide the corrected code.. Don't mind :-) – Mahendra Liya May 27 '11 at 04:56