-1

I have ProductServlet.java where get from data base List<Map<String, Object>> paramsList - full of product object with its parameters. Then I do the following: request.setAttribute("paramList", paramsList); and get the list in prodView.jsp for its parsing:

<form method="POST" action="editProduct">
...
     <c:forEach var="map" items="${paramList}" >
        <tr>
            <td>Attributes</td>
            <td><input type="text" name="attr" value="<c:out value="${map['attrName']}"/>" /></td>
        </tr>
        <tr>
            <td>Values of attributes</td>
            <td><input type="text" name="vals" value="<c:out value="${map['value']}"/>" /></td>
        </tr>
    </c:forEach>
...

It's suppose that a user modifies the paramList. So I'd like to get this modified list<Map<String, Object>> (its elements) in EditProductServlet.java (@WebServlet(urlPatterns = { "/editProduct" })). But I don't know exactly how to do this. And when I try this I get nothing:

String t = (String) request.getParameter("attr");

Thank you for help!

user3856196
  • 349
  • 4
  • 16

1 Answers1

2

You need to use getParameterValues() which returns an array of parameters with same name. Since you are creating multiple textboxes with same name i.e. attr, and vals. The method getParameterValues() of HttpSerletRequest should be used instead of getParameterValue().

Below is the complete sample

Servlet Code

public class MapServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("do Get");
        Map<String, String> values = new HashMap<String, String>();
        values.put("1", "One");
        values.put("2", "Two");
        values.put("3", "Three");
        values.put("4", "Four");

        request.setAttribute("paramList", values);
        RequestDispatcher rd = request.getRequestDispatcher("youruipage.jsp");
        rd.forward(request, response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("do Post");
        String keys[] = request.getParameterValues("attr");
        String values[] = request.getParameterValues("vals");

        for (String str1 : values) {
            System.out.println(" - " + str1);
        }

        for (String str2 : keys) {
            System.out.println(" - " + str2);
        }
    }

}

JSP snippet:

<form method="POST" action="editProduct">
<h2> test</h2>
    <table>
  <c:forEach var="map" items="${requestScope.paramList}" >
        <tr>
            <td>Attributes</td>
            <td><input type="text" name="attr" value="${map.key}" /></td>
        </tr>
        <tr>
            <td>Values of attributes</td>
            <td><input type="text" name="vals" value="${map.value}" /></td>
        </tr>
        </c:forEach>
 </table>
<input type="submit" value= "submit"/>
  </form>
Ganesh S
  • 510
  • 11
  • 19