title: Multiple Action in one Servlet
body: <p>I am new to JSP and Servlet, I have to put multiple actions in one servlet such as update, delete , search . I have added My jsp and Servlet here .I also using jquery validations. can anyone please help me. If it is my question is not clear, please comment that.</p>
  
  <pre><code>&lt;
  
  form id="form-sign-up" class="styled" action="DepartmentServlet" method="post"&gt;
                  &lt;fieldset&gt;
                      &lt;h3&gt;Department &lt;/h3&gt;
                      &lt;ol&gt;
                          &lt;li class="form-row"&gt;&lt;label&gt;Department Id:&lt;/label&gt;
                              &lt;input type="text" name="departmentId" id="departmentId" class="text-input required"/&gt;&lt;span id="errmsg"&gt;&lt;/span&gt;
                          &lt;/li&gt;
                          &lt;li class="form-row"&gt;&lt;label&gt;Department Name:&lt;/label&gt;
                              &lt;input type="text" name="departmentName" id="departmentName" class="text-input required"/&gt;
                          &lt;/li&gt;
                          &lt;li class="form-row"&gt;&lt;label&gt;Department Head:&lt;/label&gt;
                              &lt;select name="departmentHead" class="text-input required" id="myDropdown"style="width: 158px"&gt; &lt;option value=""&gt;Select Department Head&lt;/option&gt;
                                  &lt;%
  
                                      String val = "";
                                      Department dp = new Department();
                                      ResultSet rs = dp.getHeadOfDepartment();
                                      while (rs.next()) {
                                          val = rs.getString(2);
                                  %&gt;
  
                                  &lt;option value="&lt;%= val%&gt;"&gt;&lt;%= val%&gt;&lt;/option&gt;
                                  &lt;%
                                      }
  
                                  %&gt;
  
  
                              &lt;/select&gt;
                          &lt;/li&gt;
                          &lt;li class="form-row"&gt;&lt;label&gt;Appointed Date:&lt;/label&gt;
                              &lt;input id="demo1" name="appointedDate"type="text" maxlength="25" size="25" class="text-input required"&gt;&lt;a href="javascript:NewCal('demo1','ddmmmyyyy',true,24)"&gt;&lt;img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"&gt;&lt;/a&gt;
                                  &lt;span class="descriptions"&gt;pick a date..&lt;/span&gt;
                          &lt;/li&gt;
  
                          &lt;li class="button-row"&gt;
                              &lt;input type="submit" value="ADD" id="add"class="btn-submit img-swap"/&gt;
                              &lt;input type="submit" value="Delete"  class="btn-delete img-swap"/&gt;
                              &lt;input type="submit" value="Update" class="btn-update img-swap"/&gt;
                              &lt;input type="submit" value="Search" class="btn-search img-swap"/&gt;
                          &lt;/li&gt;
                      &lt;/ol&gt;
                  &lt;/fieldset&gt;
              &lt;/form&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>public class DepartmentServlet extends HttpServlet {
  
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          //Getting the Value from the viewer
          String sdepartmentId=request.getParameter("departmentId");
          String departmentName=request.getParameter("departmentName");
          String departmentHead=request.getParameter("departmentHead");
          String dateTime=request.getParameter("appointedDate");
  
  
  
  
          //Converting to the String department id to integer
          Integer departmentId=Integer.parseInt(sdepartmentId);
  
          Department dp=new Department();
          dp.setAppDate(dateTime);
          dp.setDepartHead(departmentHead);
          dp.setDepartId(departmentId);
          dp.setDepartName(departmentName);
          try {
              new Department().insertDepartment(dp);
          } catch (SQLException ex) {
              Logger.getLogger(DepartmentServlet.class.getName()).log(Level.SEVERE, null, ex);
          }
  
      }
  </code></pre>
  
  <h2>    }</h2>
  
  <pre><code>$(document).ready(function() { 
      $("#departmentId").keypress(function (e)  
      { 
          //if the letter is not digit then display error and don't type anything
          if( e.which!=8 &amp;&amp; e.which!=0 &amp;&amp; (e.which&lt;48 || e.which&gt;57))
          {
              //display error message
              $("#errmsg").html("Digits Only").show().fadeOut("slow"); 
              return false;
          }   
      });
  
  
      $('.btn-delete').click(function(e){
  
          // Declare the function variables:
          // Parent form, form URL, email regex and the error HTML
          var $formId = $(this).parents('form');
          var formAction = $formId.attr('action');
          var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
          var $error = $('&lt;span class="error"&gt;&lt;/span&gt;');
  
          // Prepare the form for validation - remove previous errors
          $('li',$formId).removeClass('error');
  
          $('span.error').remove();
  
          // Validate all inputs with the class "required"
          $('.required',$formId).each(function(){
              var inputVal = $("#departmentId").val();
              var $parentTag = $("#departmentId").parent();
              if(inputVal == ''){
                  $parentTag.addClass('error').append($error.clone().text('Required Field'));
              }
  
  
  
  
  
  
              // Run the email validation using the regex for those input items also having class "email"
  
  
              // Check passwords match for inputs with class "password"
  
  
          });
  
          // All validation complete - Check if any errors exist
          // If has errors
  
  
  
          if ($('span.error').length &gt; 0) {
  
              $('span.error').each(function(){
  
                  // Set the distance for the error animation
                  var distance = 5;
  
                  // Get the error dimensions
                  var width = $(this).outerWidth();
  
                  // Calculate starting position
                  var start = width + distance;
  
                  // Set the initial CSS
                  $(this).show().css({
                      display: 'block',
                      opacity: 0,
                      right: -start+'px'
                  })
                  // Animate the error message
                  .animate({
                      right: -width+'px',
                      opacity: 1
                  }, 'slow');
  
              });
          } else {
              $formId.submit();
          }
          // Prevent form submission
          e.preventDefault();
  
  
      });
  
      // Fade out error message when input field gains focus
      $('.required').focus(function(){
          var $parent = $(this).parent();
          $parent.removeClass('error');
          $('span.error',$parent).fadeOut();
      });
  
      $('.btn-submit').click(function(e){
  
          // Declare the function variables:
          // Parent form, form URL, email regex and the error HTML
          var $formId = $(this).parents('form');
          var formAction = $formId.attr('action');
          var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
          var $error = $('&lt;span class="error"&gt;&lt;/span&gt;');
  
          // Prepare the form for validation - remove previous errors
          $('li',$formId).removeClass('error');
  
          $('span.error').remove();
  
          // Validate all inputs with the class "required"
          $('.required',$formId).each(function(){
              var inputVal = $(this).val();
              var $parentTag = $(this).parent();
              if(inputVal == ''){
                  $parentTag.addClass('error').append($error.clone().text('Required Field'));
              }
  
  
  
  
  
  
              // Run the email validation using the regex for those input items also having class "email"
              if($(this).hasClass('email') == true){
                  if(!emailReg.test(inputVal)){
                      $parentTag.addClass('error').append($error.clone().text('Enter valid email'));
                  }
              }
  
              // Check passwords match for inputs with class "password"
              if($(this).hasClass('password') == true){
                  var password1 = $('#password-1').val();
                  var password2 = $('#password-2').val();
                  if(password1 != password2){
                      $parentTag.addClass('error').append($error.clone().text('Passwords must match'));
                  }
              }
  
          });
  
          // All validation complete - Check if any errors exist
          // If has errors
  
  
  
          if ($('span.error').length &gt; 0) {
  
              $('span.error').each(function(){
  
                  // Set the distance for the error animation
                  var distance = 5;
  
                  // Get the error dimensions
                  var width = $(this).outerWidth();
  
                  // Calculate starting position
                  var start = width + distance;
  
                  // Set the initial CSS
                  $(this).show().css({
                      display: 'block',
                      opacity: 0,
                      right: -start+'px'
                  })
                  // Animate the error message
                  .animate({
                      right: -width+'px',
                      opacity: 1
                  }, 'slow');
  
              });
          } else {
              $formId.submit();
          }
          // Prevent form submission
          e.preventDefault();
  
  
      });
  
      // Fade out error message when input field gains focus
      $('.required').focus(function(){
          var $parent = $(this).parent();
          $parent.removeClass('error');
          $('span.error',$parent).fadeOut();
      });
  
  
  
  });
  </code></pre>