Request paramter value is truncated to first part when it contains spaces

白昼怎懂夜的黑 提交于 2019-12-17 14:58:14

问题


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


回答1:


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">



回答2:


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.



来源:https://stackoverflow.com/questions/6147840/request-paramter-value-is-truncated-to-first-part-when-it-contains-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!