1

I have below jsp code.

<table id="myTable" border="0" cellspacing="0" style="border-spacing:0; width:100%;border-collapse: collapse;">
            <%
                List<Object> object = (List<Object>)request.getAttribute("myContact");
        for(int i=0;i<object.size();i++){
                MyModel myModel = (MyModel)object.get(i);
                String mail = myModel.getmail()!=null ? myModel.getmail().toString().trim() : "";
                String title = myModel.gettitle()!=null ? myModel.gettitle().toString().trim() : "";
                String name = myModel.getname()!=null ? myModel.getname().toString().trim() : "";               
            %>


            <tr>
            <td class="table-border-bottom"><label for="name">Name:</label></td>
            <td class="table-border-bottom"><input id="name" type="text" value='<%=name%>' name="name" class="required" style="height: 17px;"/>
            </td>
            <td class="table-border-bottom"><label for="contactTitle">Title:</label></td>
            <td class="table-border-bottom"> <input id="title" type="text" value='<%=title%>' name="title" class="required" style="height: 17px;"/>

            </td>
            <td class="table-border-bottom"><label for="mail">Email:</label></td>
            <td class="table-border-bottom"><input id="mail" type="text" value='<%=mail%>' name="mail" class="required email" style="height: 17px; "/>

            </td>
            </tr>

    <% } %>

            <tr align="center">
            <td valign="bottom" colspan="6" style="height: 45px; ">
            <input type="button" id="submit" name="submit" value="Save" style="width: 80px ; height:24px; text-align: center;border-radius: 10px 10px 10px 10px;"/> 
            <input type="button" id="revert" name="revert" value="Revert" style="width: 80px ; height:24px;text-align: center;border-radius: 10px 10px 10px 10px;"/></td>
            </tr>

      </table>  

To access form values i can write the code as below in servlet:

String name = request.getParameter("name");
    String title = request.getParameter("title");
    String email = request.getParameter("email"); 

But my table is populated dynamically. i will not come to know how many params it has as the form will have many fields and the form is populated by looping the list returned from db. Also, in the form, input names have been hard coded. As i will have many fields based on the db returned list, how can i avoid and give unique names for the input elements?

How can i overcome this?

Thanks!

user1016403
  • 12,151
  • 35
  • 108
  • 137
  • You can get all the parameters http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterMap%28%29 – Makky Jul 30 '13 at 12:20
  • Also it not recommended to have java code in your jsp – Makky Jul 30 '13 at 12:20
  • In your servlet you can save in an array, for solved the dynamically populated table you can insert for example and button, i have had a similar problem, but i decided dont show the consult directly i only show when somebody push a button. On the other hand i used EXTjs because is easy to use to grid displays. – Distopic Jul 30 '13 at 12:22
  • you could easily create unique field __, however, please avoid scripting in the jsp!! – fGo Jul 30 '13 at 12:24

1 Answers1

1

The most basic thing to use here is JSTL core:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<c:forEach items="${myModel}" var="${myContact}" varStatus="count">
    <tr>
        // I am setting the unique name for each input here
        <td><input type="text" name="name_${count.index}"/></td>
        <td>${myModel.name}</td>
      ........
    </tr>
</c:forEach>

I have already answered your question earlier.

Please read How to avoid Java Code in JSP-Files?.


but here accessing many inputs with same name in servlet

You can use ServletRequest#getParameterNames():

Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration.

Sample code to get all the parameters from request object in Servlet :

Enumeration allParameterNames = request.getParameterNames();
while(allParameterNames.hasMoreElements())
{
    Object object = allParameterNames.nextElement();
    String param =  (String)object;
    String value =  request.getParameter(param);
    pw.println("Parameter Name is '"+param+"' and Parameter Value is '"+value+"'");
}    

You can also use ServletRequest#getParameterMap() method .

Returns a java.util.Map of the parameters of this request. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164