title: How to take values of only selected checkbox in Action class in Struts 2 and JSP
body: <p>I am displaying 24 checkboxes. I want to get all the values of checked checkboxes in action class and insert it as a new record inside database.Inserting will be done once I succeed in getting the values of checked checkboxes on button click.</p>
  <p>I referred <a href="https://stackoverflow.com/questions/6800008/how-to-retrieve-checkbox-values-in-struts-2-action-class">this</a> link in which I followed the answer answered by steven sir but with help of that I can display only boolean values but here I want text values of checkboxes selected.</p>
  <p>So below is my JSP page.</p>
  <pre class="lang-jsp prettyprint-override"><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
      pageEncoding=&quot;ISO-8859-1&quot;%&gt;
      &lt;%@taglib uri=&quot;/struts-tags&quot; prefix=&quot;s&quot; %&gt;
  &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;s:form action=&quot;eventInsertAction&quot;&gt;
          &lt;!-- Main content --&gt;
          &lt;section class=&quot;content&quot;&gt;
            &lt;!-- Small boxes (Stat box) --&gt;
            
            
            &lt;div class=&quot;row&quot;&gt;
              &lt;div class=&quot;contetpanel&quot;&gt;
                  &lt;div&gt;
                      &lt;div class=&quot;crevtbl&quot;&gt; 
                          &lt;div class=&quot;crevtblRow&quot;&gt; 
                              &lt;div class=&quot;crevtblCell&quot;&gt;Event Name&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;:&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;&lt;input name=&quot;event.eventName&quot; class=&quot;formtextfield&quot; type=&quot;text&quot;&gt;&lt;s:fielderror fieldName=&quot;event.eventName&quot;/&gt;&lt;/div&gt; 
                          &lt;/div&gt;
                          &lt;div class=&quot;crevtblRow&quot;&gt; 
                              &lt;div class=&quot;crevtblCell&quot;&gt;Company Name&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;:&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;&lt;input name=&quot;event.companyName&quot; class=&quot;formtextfield&quot; type=&quot;text&quot; &gt;&lt;s:fielderror fieldName=&quot;event.companyName&quot;/&gt;&lt;/div&gt; 
                          &lt;/div&gt;
                          &lt;div class=&quot;crevtblRow&quot;&gt; 
                              &lt;div class=&quot;crevtblCell&quot;&gt;Contact Person Name&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;:&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;&lt;input name=&quot;event.contactPerson&quot; class=&quot;formtextfield&quot; type=&quot;text&quot; &gt;&lt;s:fielderror fieldName=&quot;event.contactPerson&quot;/&gt;&lt;/div&gt; 
                          &lt;/div&gt;
                          &lt;div class=&quot;crevtblRow&quot;&gt; 
                              &lt;div class=&quot;crevtblCell&quot;&gt;Contact&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;:&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;&lt;input name=&quot;event.contactNumber&quot; class=&quot;formtextfield&quot; type=&quot;text&quot; &gt;&lt;s:fielderror fieldName=&quot;event.contactNumber&quot;/&gt;&lt;/div&gt; 
                          &lt;/div&gt;
                          &lt;div class=&quot;crevtblRow&quot;&gt; 
                              &lt;div class=&quot;crevtblCell&quot;&gt;Email&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;:&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;&lt;input name=&quot;event.emailId&quot; class=&quot;formtextfield&quot; type=&quot;text&quot; &gt;&lt;s:fielderror fieldName=&quot;event.emailId&quot;/&gt;&lt;/div&gt; 
                          &lt;/div&gt;
                          &lt;div class=&quot;crevtblRow&quot;&gt; 
                              &lt;div class=&quot;crevtblCell&quot;&gt;Event Venue&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;:&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;&lt;input name=&quot;event.eventVenue&quot; class=&quot;formtextfield&quot; type=&quot;text&quot; &gt;&lt;s:fielderror fieldName=&quot;event.eventVenue&quot;/&gt;&lt;/div&gt; 
                          &lt;/div&gt;
                          &lt;div class=&quot;crevtblRow&quot;&gt; 
                              &lt;div class=&quot;crevtblCell&quot;&gt;Event Date&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;:&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;From : &lt;input name=&quot;event.fromDate&quot; class=&quot;formtextfield1&quot; type=&quot;text&quot; placeholder=&quot;YYYY/MM/DD&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; To : &lt;input name=&quot;event.toDate&quot; class=&quot;formtextfield1&quot; type=&quot;text&quot; placeholder=&quot;YYYY/MM/DD&quot;&gt;&lt;/div&gt; 
                          &lt;/div&gt;
                          &lt;div class=&quot;crevtblRow&quot;&gt; 
                              &lt;div class=&quot;crevtblCell&quot;&gt;Event Time&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;:&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;&lt;input name=&quot;event.eventTime&quot; class=&quot;formtextfield1&quot; type=&quot;text&quot; placeholder=&quot;HH:MM  AM/PM&quot; &gt;&lt;s:fielderror fieldName=&quot;event.eventTime&quot;/&gt;&lt;/div&gt; 
                          &lt;/div&gt;
                          &lt;div class=&quot;crevtblRow&quot;&gt;
                              &lt;div class=&quot;crevtblCell&quot;&gt;License Required&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;:&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Rangabhoomi&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Fire NOC&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Fire Engine&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Premises &amp;amp; NOC&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Performance&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;PWD&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Local Police&lt;/span&gt;&lt;br /&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Collector&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;PPL&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;IPRS&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Traffic&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Liquor License&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Ticket Selling License&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;BMC Parking&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Parking&lt;/span&gt;&lt;br /&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Port Trust&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Novex&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Foreign Artist&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;DCP Office&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Fire Marshal&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Sale Tax NOC&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Other&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Extra&lt;/span&gt;
                              &lt;input name=&quot;event.licenserequired&quot; type=&quot;checkbox&quot; class=&quot;formcheckbox&quot; value=&quot;&quot;&gt;&lt;span class=&quot;formcheckbox_content&quot;&gt;Commission&lt;/span&gt;
                                  
                              &lt;/div&gt; 
                          &lt;/div&gt;
                          &lt;div class=&quot;crevtblRow&quot;&gt; 
                              &lt;div class=&quot;crevtblCell&quot;&gt;&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell1&quot;&gt;&lt;/div&gt; 
                              &lt;div class=&quot;crevtblCell2&quot;&gt;&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Create Event&lt;/button&gt;&lt;/div&gt; 
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
              
  
              &lt;/section&gt;
              &lt;/s:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;  
  </code></pre>
  <p><strong>Below is my setters and getters class</strong></p>
  <pre class="lang-java prettyprint-override"><code>package com.ca.pojo;
  
  public class Event {
      public Event() {
          // TODO Auto-generated constructor stub
      }
  
      private String eventName;
      private String companyName;
      private String contactPerson;
      private String contactNumber;
      private String emailId;
      private String eventVenue;
      private String fromDate;
      private String toDate;
      private String eventTime;
      private String licenserequired;
      
      public String getEventName() {
          return eventName;
      }
  
      public void setEventName(String eventName) {
          this.eventName = eventName;
      }
  
      public String getCompanyName() {
          return companyName;
      }
  
      public void setCompanyName(String companyName) {
          this.companyName = companyName;
      }
  
      public String getContactPerson() {
          return contactPerson;
      }
  
      public void setContactPerson(String contactPerson) {
          this.contactPerson = contactPerson;
      }
  
      public String getContactNumber() {
          return contactNumber;
      }
  
      public void setContactNumber(String contactNumber) {
          this.contactNumber = contactNumber;
      }
  
      public String getEmailId() {
          return emailId;
      }
  
      public void setEmailId(String emailId) {
          this.emailId = emailId;
      }
  
      public String getEventVenue() {
          return eventVenue;
      }
  
      public void setEventVenue(String eventVenue) {
          this.eventVenue = eventVenue;
      }
  
      public String getFromDate() {
          return fromDate;
      }
  
      public void setFromDate(String fromDate) {
          this.fromDate = fromDate;
      }
  
      public String getToDate() {
          return toDate;
      }
  
      public void setToDate(String toDate) {
          this.toDate = toDate;
      }
  
      public String getEventTime() {
          return eventTime;
      }
  
      public void setEventTime(String eventTime) {
          this.eventTime = eventTime;
      }
  
      public String getLicenserequired() {
          return licenserequired;
      }
  
      public void setLicenserequired(String licenserequired) {
          this.licenserequired = licenserequired;
      }
      
  }  
  </code></pre>
  <p><strong>Below is my Action class</strong></p>
  <pre class="lang-java prettyprint-override"><code>package com.ca.actions;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  import com.ca.database.Database;
  import com.ca.pojo.Event;
  import com.opensymphony.xwork2.ActionSupport;
  
  public class EventInsertAction extends ActionSupport {
  
      private String eventId;
      Event event;
      String name;
  
      public EventInsertAction() {
          // TODO Auto-generated constructor stub
      }
  
      
      public String getName() {
          return name;
      }
  
  
      public void setName(String name) {
          this.name = name;
      }
  
  
      public String getEventId() {
          return eventId;
      }
  
      public void setEventId(String eventId) {
          this.eventId = eventId;
      }
  
      public Event getEvent() {
          return event;
      }
  
      public void setEvent(Event event) {
          this.event = event;
      }
  
      @Override
      public String execute() throws Exception {
          
          System.out.println(&quot;Event&quot;+event.getLicenserequired());
  
          // TODO Auto-generated method stub
          System.out.println(&quot;Hi Mahi&quot;+name);
  
          List&lt;Integer&gt; ints = new ArrayList&lt;Integer&gt;();
          int i = 0;
  
          for (int i1 = 0; i1 &lt; 10000; i1++) {
              ints.add(i1);
          }
          // Collections.shuffle(ints);
  
          String Id = String.valueOf(ints.get(i++));
  
          eventId = event.getEventName() + Id;
      
          System.out.println(eventId);
          try {
              Database database = new Database();
              Connection con = database.Get_Connection();
              System.out.println(&quot;Driver Loaded&quot;);
              PreparedStatement st = con
                      .prepareStatement(&quot;insert into event(EVENT_ID,EVENT_NAME,COMPANY_NAME,CONTACT_PERSON,CONTACT_NO,EMAIL_ID,EVENT_VENUE,FROM_DATE,TO_DATE,EVENT_TIME)&quot;
                              + &quot;values(?,?,?,?,?,?,?,?,?,?)&quot;);
              st.setString(1, eventId);
              st.setString(2, event.getEventName());
              st.setString(3, event.getCompanyName());
              st.setString(4, event.getContactPerson());
              st.setString(5, event.getContactNumber());
              st.setString(6, event.getEmailId());
              st.setString(7, event.getEventVenue());
              st.setString(8, event.getFromDate());
              st.setString(9, event.getToDate());
              st.setString(10, event.getEventTime());
              st.executeUpdate();
              System.out.println(&quot;success&quot;);
              con.close();
          } catch (Exception e) {
              System.out.println(e);
          }
  
          return &quot;success&quot;;
      }
  
      @Override
      public void validate() {
          // TODO Auto-generated method stub
          super.validate();
          if (event.getEventName().isEmpty()) {
              System.out.println(&quot;Event Name&quot;);
              addFieldError(&quot;event.eventName&quot;, &quot;Please Enter Event Name ..&quot;);
          }
          
          if (event.getCompanyName().isEmpty()) {
              addFieldError(&quot;event.companyName&quot;, &quot;Please Enter Company Name.. &quot;);
          }
          
          if (event.getContactNumber().isEmpty()) {
              addFieldError(&quot;event.contactNumber&quot;,
                      &quot;Please Enter Contact Number..&quot;);
          } else {
              String expression = &quot;^\\+?[0-9\\-]+\\*?$&quot;;
              CharSequence inputStr = event.getContactNumber();
              Pattern pattern = Pattern.compile(expression,
                      Pattern.CASE_INSENSITIVE);
              Matcher matcher = pattern.matcher(inputStr);
              if (!matcher.matches())
                  addFieldError(&quot;event.contactNumber&quot;, &quot;Invalid Contact Number..&quot;);
  
          }
          
          if (event.getContactPerson().isEmpty()) {
              addFieldError(&quot;event.contactPerson&quot;,
                      &quot;Please Enter Contact Person Name..&quot;);
          }
          
          if (event.getEmailId().isEmpty()) {
              addFieldError(&quot;event.emailId&quot;, &quot;Please Enter Email ID..&quot;);
          } else {
              String expression = &quot;^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$&quot;;
              CharSequence inputStr = event.getEmailId();
              Pattern pattern = Pattern.compile(expression,
                      Pattern.CASE_INSENSITIVE);
              Matcher matcher = pattern.matcher(inputStr);
              if (!matcher.matches())
                  addFieldError(&quot;event.emailId&quot;, &quot;Invalid Email Address..&quot;);
          }
          
          if (event.getEventVenue().isEmpty()) {
              addFieldError(&quot;event.eventVenue&quot;, &quot;Please Enter Event Venue..&quot;);
          }
          
          if (event.getFromDate().isEmpty()) {
              addFieldError(&quot;event.fromDate&quot;, &quot;Please Enter Date..&quot;);
          }
          
          if (event.getToDate().isEmpty()) {
              addFieldError(&quot;event.toDate&quot;, &quot;Please Enter To Date..&quot;);
          }
          
          if (event.getEventTime().isEmpty()) {
              addFieldError(&quot;event.eventTime&quot;, &quot;Please Enter Event Time..&quot;);
          }
  
      }
  
  }  
  </code></pre>