wood burning stoves 2.0*
The moose likes Servlets and the fly likes Getting a value from a checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
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: 388
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: 60741
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

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: 388
yes, that helps. Thanks
 
 
subject: Getting a value from a checkbox
 
Similar Threads
Servlet and ajax
Valid JSP Parameters
servlet(delete)
java check box
Retaining value of checkbox when returning back from a servlet