• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie: Need help with checkbox post parameters

 
kan tao
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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();
}
}
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice .... here is my code with tags


 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael Angstadt


It was due to the space before "item \".
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to help!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic