Wednesday, 9 April 2014

Servlets : Form Data - Checkbox Example


Click here to watch in Youtube :  https://www.youtube.com/watch?v=t5hLQebag3g

Click the below Image to Enlarge

ServletFormDataCheckBox Project Dir Structure

CheckBoxServlet.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckBoxServlet extends HttpServlet
{

 private static final long serialVersionUID = 1L;

 public void init()
 {
  System.out
          .println("-----------------------------------------------------");
  System.out
          .println("init method has been called and servlet is initialized");
  System.out
          .println("-----------------------------------------------------");
 }

 // Method to handle GET method request.
 public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
 {
  // Set response content type
  response.setContentType("text/html");

  PrintWriter out = response.getWriter();

  out.println("\nRead data using '<b>String[] getParameterValues(String name)</b>' method \n");

  String[] values = request.getParameterValues("bookname");

  out.println("<br><br>Selected Books are : <br>");

  out.println("<ul>");

  for (String value : values)
  {
   out.print(" <li><b>" + value + "</b>");
  }

  out.println("</ul>");
 }

 // Method to handle POST method request.
 public void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException
 {
  doGet(request, response);
 }

 public void destroy()
 {
  System.out
          .println("-----------------------------------------------------");
  System.out
          .println("destroy method has been called and servlet is destroyed");
  System.out
          .println("-----------------------------------------------------");
 }
}

web.xml    
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0">
  <display-name>ServletForm GET</display-name>
  <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>
  <servlet>
    <servlet-name>books</servlet-name>
    <servlet-class>CheckBoxServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>books</servlet-name>
    <url-pattern>/books</url-pattern>
  </servlet-mapping>
</web-app>

index.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<!DOCTYPE HTML><html lang="en">
<head>
<meta charset="UTF-8">
<title>Select Books</title>
</head>
<body>
 <p>
  <h3>Select Books</H3>
 <p></p>
 <ul>
  <li><a href="books.html">Select Books</a></li>
 </ul>
</body>
</html>

books.html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<html>
<body>
<form action="books" method="GET">

 <input type="checkbox" name="bookname" value="Java" /> Java
 <input type="checkbox" name="bookname"  value="Hibernate" /> Hibernate
 <input type="checkbox" name="bookname" value="Webservice" /> Webservice
 <input type="submit" value="Select Book" />
</form>
</body>
</html>

Environment Used

JDK version :1.6.0_30
Tomcat version : 7.0.50 

To Download ServletFormDataCheckBoxApp Project Click the below link
https://sites.google.com/site/javaee4321/servlets/ServletFormDataCheckBoxApp.zip?attredirects=0&d=1

 See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • No comments:

    Post a Comment