Results 1 to 4 of 4
  1. #1
    sashi799 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default Newbie: Need help with checkbox post parameters

    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();
    }
    }
    }

  2. #2
    sashi799 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default

    Resubmitting the code:

    Java 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
    		{
    			String it = request.getParameter("item");
    			out.println(it);
    			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[] 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();
    		}
    	}
    }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Is the check box checked?
    If not then it won't be sent.

  4. #4
    sashi799 is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Talking

    Ok got the solution ...its due to the space after "item \" ---> "item\" :D

Similar Threads

  1. CheckBox
    By boss in forum NetBeans
    Replies: 3
    Last Post: 01-17-2010, 04:51 AM
  2. CheckBox in JTable
    By nikosa in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2009, 05:01 AM
  3. Checkbox to list
    By msixty in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 08:10 PM
  4. Replies: 3
    Last Post: 05-04-2009, 03:15 AM
  5. checkbox
    By Alan in forum AWT / Swing
    Replies: 3
    Last Post: 05-18-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •