aspose file tools
The moose likes Servlets and the fly likes Getting a value from a checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Getting a value from a checkbox" Watch "Getting a value from a checkbox" New topic
Author

Getting a value from a checkbox

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 375
I can't seem to retrieve a value from a checkbox. It is either checked or unchecked, a boolean. So how do I extract its value when it reaches the servlet?

<INPUT type="checkbox" value="guessorall" checked>

then once the form is submitted and sent to the servlet I do this...

String guessall = req.getParameter("guessorall");

It always returns null.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56157
    
  13

You need to set the name attribute of the checkbox. The name will become the request parameter, and if checked, its value will be the value of the value attribute. if unchecked, no request parameter will be created for it.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56157
    
  13

For example



This will result, if checked, in a request parameter named 'xyz' whose value is the string 'true' (easily converted to a boolean). If unchecked, no 'xyz' parameter will be present.
Kiran Sonaje
Greenhorn

Joined: Aug 23, 2004
Posts: 23
Hi,
This code may help u
All the best


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class HttpRequestDemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Obtaining Multi-Value Parameters</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<BR>");
out.println("<BR>Select your favorite music:");
out.println("<BR><FORM METHOD=POST>");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Rock>Rock");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Jazz>Jazz");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Classical>Classical");
out.println("<BR><INPUT TYPE=CHECKBOX " +
"NAME=favoriteMusic VALUE=Country>Country");
out.println("<BR><INPUT TYPE=SUBMIT VALUE=Submit>");
out.println("</FORM>");
out.println("</BODY>");
out.println("</HTML>");
}
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String[] values = request.getParameterValues("favoriteMusic");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if (values != null ) {
int length = values.length;
out.println("You have selected: ");
for (int i=0; i<length; i++) {
out.println("<BR>" + values[i]);
}
}
}
}
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 375
yes, that helps. Thanks
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Getting a value from a checkbox
 
Similar Threads
Retaining value of checkbox when returning back from a servlet
java check box
servlet(delete)
Valid JSP Parameters
Servlet and ajax