user1016403
user1016403

Reputation: 12621

How to access form values in a servlet?

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!

Upvotes: 1

Views: 5545

Answers (1)

AllTooSir
AllTooSir

Reputation: 49372

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.

Upvotes: 1

Related Questions