Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a map or objects from jsp to a servlet

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.

like image 727
Mr. Singthoi Avatar asked Nov 15 '25 12:11

Mr. Singthoi


2 Answers

Write all your code inside form tag.

Use this code :

<c:forEach var="configParams" items="${configParamsMap}" varStatus="itemsRow">
   <tr>
        <td>
        <c:out value="${configParams.key}" />
        </td>
        <td><input type="text" name="" value="${configParams.value}" /></td>
  </tr>
</c:forEach>

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.

like image 55
Muhammad Imran Tariq Avatar answered Nov 17 '25 09:11

Muhammad Imran Tariq


Well you cannot pass a Map through a HTTP request. I suppose you want to keep track of all the changes especially the delete from the JSP to the server side

So instead of storing it in request , store it in session In the JSP

session.setAttribute("configParamsMap", configParamsMap);

And in your servlet get the id to be deleted from the request

    String idToDelete = request.parameter("id");

    //Now delete the id from the map
    Map<String,String> configParamsMap = (Map<String,String>)session.getAttribute("configParamsMap");

//Delete it from the map
configParamsMap.remove(idToDelete );
like image 23
Sudhakar Avatar answered Nov 17 '25 08:11

Sudhakar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!