Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

dynamically loop through form parameters in servlet

jonnyd91Oct 26 2008 — edited Oct 27 2008
I have a html table that has 4 columns. This table is built from the database and can have any number of rows. For the first column of every row there is a check box. What I need to do is when this form gets posted to my servlet i need to loop though and get the parameters for each row of the table and update the corresponding row in my database. Below is the form i am using. I am not sure the best way to do this in the servlet and i am looking for some direction. Also does each of the rows components have to have a different name?
<form action="<c:url value='savebrands' />" name="saveBrands" method="post" STYLE="margin:0; padding:0;">
  <table width="694"  border="0" align="left" cellpadding="1" cellspacing="1" style="border: 1px solid #000000">
    <tr>
      <td width="87" height="19" align="center" valign="middle" bgcolor="#787878"><span class="style1">Show On Web</span></td>
      <td width="197" align="center" valign="middle" bgcolor="#787878"><span class="style1">Brand Name</span></td>
      <td align="center" valign="middle" bgcolor="#787878"><span class="style1">Nav Image Filename</span></td>
      <td align="center" valign="middle" bgcolor="#787878"><span class="style1">Brand Header Filename</span></td>
    </tr>
    <c:forEach var="vendor" items="${vendors}">
      <tr>
        <td height="24" align="center" valign="middle">
          <input type="checkbox" name="${vendor.vCode}" id="checkbox" />
        </td>
        <td height="24" align="left" valign="middle">${vendor.vName}</td>
        <td width="198" height="24" align="center" valign="middle"><input type="text" name="${vendor.vLogo}" id="textfield" value="${vendor.vLogo}" /></td>
        <td width="197" align="center" valign="middle"><input type="text" name="${vendor.vHeader}" id="textfield" value="${vendor.vHeader}" /></td>
      </tr>
    </c:forEach>
    <tr>
      <td height="4" colspan="4" align="right" valign="middle"><hr /></td>
    </tr>
    <tr>
      <td height="42" colspan="4" align="right" valign="middle">
          <input name="editBtn" type="image" src="adminImages/save.gif" width="100" height="40" />
    </tr>
  </table>
</form>
Servlet Attempt
  public class SaveBrandsServlet extends HttpServlet
{

	public void doPost(HttpServletRequest request,
			HttpServletResponse response)
	throws IOException, ServletException
	{
		HttpSession session = request.getSession();
		
		   Enumeration keys = request.getParameterNames();
		   while (keys.hasMoreElements() )
		   {
		      String key = (String)keys.nextElement();
		      System.out.println(key);

		      //To retrieve a single value
		      String value = request.getParameter(key);
		      System.out.println(value);

		      // If the same key has multiple values (check boxes)
		      String[] valueArray = request.getParameterValues(key);
		      
		      for(int i = 0; i > valueArray.length; i++){
		    	  System.out.println("VALUE ARRAY" + valueArray);
}
}

AdminUser user = (AdminUser)session.getAttribute("user");

String url = "";

if(user == null){
url = "/adminLogin.jsp";
}
else{
ArrayList<Vendor> vendors = VendorDB.selectVendors();
session.setAttribute("vendors", vendors);
session.setAttribute("user", user);
url = "/brands.jsp";
}

RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
Edited by: jonnyd9191 on Oct 26, 2008 9:09 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 24 2008
Added on Oct 26 2008
7 comments
1,008 views