I have an application which passes a map from a servlet to a jsp. In the jsp,i displays the map and provides an option to delete or edit the values pf the map.But, after changing the values, how to send the the map back to another servlet,where it recieves the map.
Suppose, i have a servet “servletA” which passes a map to a jsp as follows:
public int Id=11111;
Map<String,String> configParamsMap=new HashMap<String,String>(size);
configParamsMap.put("1", "arg1");
configParamsMap.put("2", "arg2");
configParamsMap.put("3", "arg3");
configParamsMap.put("4", "arg4");
//
System.out.println("parameters passing to the jsp:: appId"+appId+"::configId"+configId);
request.setAttribute("configParamsMap", configParamsMap);
request.setAttribute("Id", Id);
RequestDispatcher rd = request.getRequestDispatcher("/JSP/display.jsp");
rd.forward(request, response);
in the jsp, i can do delete or edit the values. i am doing delete as follows and passing the parameters
<c:forEach var="configParams" items="${configParamsMap}">
<!-- KEY: ${configParams.key} - VALUE: ${configParams.value} -->
<tr>
<td>
<c:out value="${configParams.key}" />
</td>
<td><input type="text" name="" value="${configParams.value}" /></td>
</tr>
</c:forEach>
</table>
<form action="sevletB?action=Delete" method="post"><input
type="submit" value="Delete"></input>
<input type="hidden" name="Id" value="${Id}"></input>
</form>
My problem is that how to pass the map back to another servlet “servletB” as i have done to parameter “id” . This map should be the one, where a user has either edited some values i.e. the present status of the map in the jsp.
Write all your code inside form tag.
Use this code :
Use a hidden field that will contain
${configParams.key}value. Use loop iterator${itemsRow.index}to make distinguished parameter names like<input type="text"name="configParam.${itemsRow.index}"value="${configParams.value}" />When form will be submitted then you can access all these values from request by giving names in
getParameter('')method.