【问题标题】:How do i extract input values with the same name into a servlet? [duplicate]如何将具有相同名称的输入值提取到 servlet 中? [复制]
【发布时间】:2014-08-10 10:11:49
【问题描述】:

我有一个包含各种 productID 值的表单。它们包含在输入文本中,具有相同名称的不同值。

<%
ListIterator ul2 = myCartList3.listIterator(); 
while(ul2.hasNext()){ 
    ShoppingCart myCart2 = new ShoppingCart();
    myCart2 = (ShoppingCart)ul2.next();
%>
<input type="text" value="<%=myCart2.getProductID() %>" name="productID"   size="3" />
<% 
} 
%>

提交表单后,它会转到 servlet。通常我会使用request.getParameter,但它只显示一个属性。如何检索多个同名属性?

【问题讨论】:

    标签: java jsp servlets request


    【解决方案1】:

    你可以使用

    getParameterValues()

    String[] productIDs= request.getParameterValues("productID");
    

    【讨论】:

      【解决方案2】:

      getParameterJavadoc 明确表示

      只有在确定参数只有一个值时才应使用此方法。如果参数可能有多个值,请使用 getParameterValues(java.lang.String)。

      getParameterValues 返回一个String[],这就是你想要的。

      【讨论】:

        猜你喜欢
        • 2012-12-30
        • 2014-05-06
        • 1970-01-01
        • 1970-01-01
        • 2018-02-22
        • 1970-01-01
        • 1970-01-01
        • 2017-08-03
        • 2021-09-06
        相关资源
        最近更新 更多