Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 22 March 2014

Servlet – Read check box value

In this tutorial we are going to see about submit form with check box and read the check box value in Servlet.
Step by step instruction given below.
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called ServletExample as given below.
Servlet-dynamic web project
3. Create package called com.javatutorialscorner.servlet under ServletExample.
4. Create Servlet called CheckBoxFormSubmit as shown in figure.
Create Servlet
http servlet 
5. Click Next it will show URL mapping.You can edit Servlet URL if you need.
6. Click Next it will show methods available  in HttpServlet. Select appropriate method you need.
http servlet Services
7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml
Servlet Mapping
1.<servlet>
2.    <servlet-name>Your Servlet Name</servlet-name>
3.    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class>
4.  </servlet>
5.  <servlet-mapping>
6.    <servlet-name>Your Servlet Name</servlet-name>
7.    <url-pattern>/URL to Call Servlet</url-pattern>
8.  </servlet-mapping>

web.xml

01.<?xml version="1.0" encoding="UTF-8"?>
03.  <display-name>ServetExample</display-name>
04.  <welcome-file-list>
05.    <welcome-file>index.html</welcome-file>
06.    <welcome-file>index.htm</welcome-file>
07.    <welcome-file>index.jsp</welcome-file>
08.    <welcome-file>default.html</welcome-file>
09.    <welcome-file>default.htm</welcome-file>
10.    <welcome-file>default.jsp</welcome-file>
11.  </welcome-file-list>
12.  <servlet>
13.    <servlet-name>CheckBoxFormSubmit</servlet-name>
14.    <servlet-class>com.javatutorialscorner.servlet.CheckBoxFormSubmit</servlet-class>
15.  </servlet>
16.  <servlet-mapping>
17.    <servlet-name>CheckBoxFormSubmit</servlet-name>
18.    <url-pattern>/CheckBoxFormSubmit</url-pattern>
19.  </servlet-mapping>
20.</web-app>

8. Add the required code inside doPost() method.

CheckBoxFormSubmit.java

01.package com.javatutorialscorner.servlet;
02. 
03.import java.io.IOException;
04.import java.io.PrintWriter;
05. 
06.import javax.servlet.ServletException;
07.import javax.servlet.http.HttpServlet;
08.import javax.servlet.http.HttpServletRequest;
09.import javax.servlet.http.HttpServletResponse;
10. 
11./**
12. * Servlet implementation class CheckBoxFormSubmit
13. */
14. 
15.public class CheckBoxFormSubmit extends HttpServlet {
16. private static final long serialVersionUID = 1L;
17. 
18. /**
19.  * @see HttpServlet#HttpServlet()
20.  */
21. public CheckBoxFormSubmit() {
22.  super();
23.  // TODO Auto-generated constructor stub
24. }
25. 
26. /**
27.  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
28.  *      response)
29.  */
30. protected void doGet(HttpServletRequest request,
31.   HttpServletResponse response) throws ServletException, IOException {
32.  // TODO Auto-generated method stub
33. }
34. 
35. /**
36.  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
37.  *      response)
38.  */
39. protected void doPost(HttpServletRequest request,
40.   HttpServletResponse response) throws ServletException, IOException {
41.  // TODO Auto-generated method stub
42.  response.setContentType("text/html");
43.  PrintWriter writer = response.getWriter();
44.  writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
45.    + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet - Submit Checkbox Data</title>"
46.    + "</head><body><h1>Java Tutorials Corner - Servlet - Submit Checkbox Data</h1><table><tr>"
47.    + "<td>Servlet Tutorial : </td><td>"
48.    + request.getParameter("servlet")
49.    + "</td></tr><tr><td>Spring Tutorial : </td><td>"
50.    + request.getParameter("spring")
51.    + "</td></tr>"
52.    + "<tr><td>Web Service Tutorial : </td><td>"
53.    + request.getParameter("webservice")
54.    + "</td></tr></table></body></html>");
55. }
56. 
57.}

9. Create html page in WebContent folder

checkboxform.html

01.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
02.<html>
03.<head>
04.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
05.<title>Java Tutorials Corner - Submit Checkbox Data</title>
06.</head>
07.<body>
08.<h1>Java Tutorials Corner - Servlet - Submit Checkbox Data</h1>
09.<form action="CheckBoxFormSubmit" method="post">
10. 
11.<table>
12.<tr>
13.<td><input type="checkbox" name="servlet" checked="checked"/> Servlet Tutorial</td>
14.</tr>
15.<tr>
16.<td><input type="checkbox" name="spring" checked="checked"/> Spring Tutorial</td>
17.</tr>
18.<tr>
19.<td><input type="checkbox" name="webservice"/>Web Service Tutorial</td>
20.</tr>
21.</table>
22.<input type="submit" value="Submit"/>
23.</form>
24.</body>
25.</html>

In above html page in form tag method="post" to mention the form submit using post method.The default method to submit form is GET.

10. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse) 
http://www.javatutorialcorner.com/2014/03/how-to-configure-tomcat-in-eclipse.html

11.call the following URL.

http://localhost:8080/ServletExample/checkboxform.html

Output

HTML page - Check box Form

checkbox formAfter form Submit

check box servlet output

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Servlet – Read check box value In this tutorial we are going to see about submit form with check box and read the check box value in Servlet. Step by step instruction given below. 1. Create new Dynamic web project by choosing File –> New –> Dynamic … Read More
  • Servlet – SendRedirect with example In this tutorial we are going to see sendRedirect in servlet with example program. Send Redirect void sendRedirect(java.lang.String location) throws java.io.IOException Sends a temporary redirect response to the client usi… Read More
  • Servlet - Hello World Sevlet using @WebServlet Annotation In this tutorial we are going to see about Hello World Servlet  program using Servlet 3 @WebServlet Annotation. public @interface WebServlet Annotation used to declare a servlet. This annotation is processed by the c… Read More
  • Servlet – set Header values in Response In this tutorial we are going to see how to set header information in servlet response with example program. Click here to read more about HTTP/1.1 Header param with Definitions List of Response Headers Header … Read More
  • Servlet – Request Dispatcher forward In this tutorials we are going to see about Servlet Request Dispatcher forward with example program. void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException Forwards a reques… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Servlet – Read check box value Rating: 5 Reviewed By: eHowToNow