aspose file tools*
The moose likes Servlets and the fly likes Newbie: Need help with checkbox post parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Newbie: Need help with checkbox post parameters" Watch "Newbie: Need help with checkbox post parameters" New topic
Author

Newbie: Need help with checkbox post parameters

kan tao
Greenhorn

Joined: Jul 09, 2010
Posts: 9
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kan tao
Greenhorn

Joined: Jul 09, 2010
Posts: 9
Thanks for the advice .... here is my code with tags


Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

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.


SCJP 6 || SCWCD 5
kan tao
Greenhorn

Joined: Jul 09, 2010
Posts: 9
Thanks Michael Angstadt


It was due to the space before "item \".
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Glad to help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Newbie: Need help with checkbox post parameters