• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Getting a value from a checkbox

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, that helps. Thanks
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic