Passing map or objects from jsp to servlet

I have an application that passes a map from a servlet to a jsp. In jsp, i displays the map and provides the ability to delete or edit pf values ​​on the map. But after changing the values, how do I send the map back to another servlet where it gets the map.

Let's say I have a servlet "servletA" that passes a map to jsp like this:

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 jsp, I can remove or change values. i am doing delete as follows and passing 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 how to pass the map back to another servlet "servletB" as I did with the "id" parameter. This map should be the one where the user either edited some values, that is, the current status of the map in jsp.

+3


source to share


3 answers


Enter all your code inside the 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 the value ${configParams.key}

. Use a loop iterator ${itemsRow.index}

to make outstanding parameter names like

<input type="text"name="configParam.${itemsRow.index}"value="${configParams.value}" />

When the form is submitted, you can access all of these values ​​from the request by specifying the names in the method getParameter('')

.

+4


source


  • You may have hidden <input type=Select>

  • Enter card values
<select name="mapname" type="hidden??">
    <c:forEach items="${mapitem}" var="mapname">
        <option value="${mapitem.key}">${mapitem.value}</option>
    </c:forEach>
</select>

      



  • send to servletB (read in servletB)
+1


source


Well, you cannot pass a card through an HTTP request. I assume you want to track all changes, especially deletions from the JSP to the server side.

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

session.setAttribute("configParamsMap", configParamsMap);

      

And in your servlet get the id that will be removed 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 );

      

+1


source







All Articles