Java – pass a map or object from a JSP to a servlet

I have an application that passes the mapping from servlet to JSP In JSP, I display the map and provide the option to delete or edit the map value However, after changing the value, how to send the map back to another servlet, where it receives the map

Suppose I have a service "servleta", which passes the map to 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 JSP, I can delete or edit values I am doing the following to delete and pass 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 how to pass the mapping back to another servlet "servletb", just as I did with the parameter "Id" The mapping should be the mapping that the user has edited some values, that is, the current state of the mapping in JSP

Solution

Write all your code in 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 hidden fields containing ${configparams. Key} values Use the loop iterator ${itemsrow. Index} to create distinguished parameter names

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

When submitting the form, you can access all these values from the request by giving a name in the getparameter (") method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>