• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Newbie: Need help with checkbox post parameters

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have search almost everything but still i cant solved the problem.

I'm just trying to print the parameter return from the checkbox but its showing NULL every time [for the checked box ]. In the following code getParameterMap is printing the correct value but getParameter and getParameterValues both returning NULL

following is my code :

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CheckboxTest extends HttpServlet
{

public void doGet (HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>Enjoy Shopping</TITLE>");
out.println("</HEAD>");
out.println("<BODY >");
out.println("<br><br>");
out.println("<P align=center><BIG><BIG>Choose the item to buy</BIG></BIG></P><br><br><br>");
out.println ("<form action=\"/demo/checkboxtest\" method=\"post\">");
out.println ("<div align=\"center\"><br>");
out.println ("<input type=\"checkbox\" name=\"item \" value=\"Item 1\" > Item 1 <br>");
out.println ("<input type=\"checkbox\" name=\"item \" value=\"Item 2\" > Item 2 <br>");
out.println("<input type=\"submit\" value=\"Add to Cart\">");
out.println ("</div>");
out.println ("</form>");
out.println("</BODY></HTML>");
out.close();
}

public void doPost (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
PrintWriter out = response.getWriter();
try
{

Map paramMap = request.getParameterMap();
Object[] keys = paramMap.keySet().toArray();
String[] values = new String[keys.length];

for(int i=0; i<keys.length; i++)
{
values[i] = ((String[])paramMap.get(keys[i]))[0];
out.println(keys[i]+" -> "+values[i]+"");
}

String it = request.getParameter("item");
out.println(it);

String[] item = request.getParameterValues("item");
if (item != null)
{
for (int i = 0; i < item.length; i++)
{
out.println (item[i]);
}
}
else out.println ("none");

}
catch (Exception e)
{
e.printStackTrace();
}
}
}
 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
 
kan tao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice .... here is my code with tags


 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that there's a space in the checkbox name in the HTML ("item ") might have something to do with it. Can you post the exact output you're getting?

Also, note that it's not good practice to put HTML in a servlet. It's better to use a JSP because it allows you to edit the page without recompiling the application and makes it much easier to read the HTML. Use a RequestDispatcher to forward the request from the servlet to the JSP.
 
kan tao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael Angstadt


It was due to the space before "item \".
 
Michael Angstadt
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to help!
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic