-1

I have aN HTML form where I have check boxes. In the Servlet, I am trying to print all the Parameter names with Values and In case it has no value i.e it's unchecked , print "BLANK", But surprisingly, the parameter itself is not printed if it has no value/is unchecked.Follows the code:

Enumeration paramNames = request.getParameterNames();

while(paramNames.hasMoreElements()) {
   String paramName = (String)paramNames.nextElement();
   out.print("<tr><td>" + paramName + "</td>\n<td>");
   String[] paramValues = request.getParameterValues(paramName);

   if(paramValues.length == 0)
      out.println("BLANK");      //Why does this not work?

   // Read single valued data
   if (paramValues.length == 1) {
      String paramValue = paramValues[0];
      if (paramValue.length() == 0)
         out.println("<i>No Value</i>");
      else
         out.println(paramValue);
    } 
    else {
      // Read multiple valued data
      out.println("<ul>");
      for(int i=0; i < paramValues.length; i++) {
         out.println("<li>" + paramValues[i]);
      }
      out.println("</ul>");
    }
}
      out.println("</tr>\n</table>\n</body></html>");
}
xcoder
  • 1,336
  • 2
  • 17
  • 44
Simran
  • 539
  • 2
  • 8
  • 28
  • What is the error that you are getting? – User27854 Dec 10 '14 at 09:29
  • What does: `out.println("-"+paramValue+"-");` instead of `out.println(paramValue);` give you? Also: if you use if-else, use `{}` it's no problem to omit them if you only have "if". But use {} if you have if else. – beardhatcode Dec 10 '14 at 09:31
  • also give the html form and the output you are getting. your error seems to be logically, it can only be answered with above things mentioned. Moreover, paramValues.length cannot be `0` because it has either `on` or `off` in case of check boxes, which is value. – saikumarm Dec 10 '14 at 09:31

2 Answers2

1

The checkboxes that are NOT checked in a form post are NOT sent as parameters. Only checked checkboxes are posted in the HTTP POST request.

If you want to post the "unchecked" check boxes, you will have to emply a small hack. Please refer this SO post.

Community
  • 1
  • 1
Manish Maheshwari
  • 4,045
  • 2
  • 15
  • 25
0

Instead of manually looking for each property Use JSTL

tags , where you can just specify the java class and with same field name as in form and values directly maps to the bean