title: show alert No data found between dates while calling ajax
body: <p>i am using java Servlet for server side programming, i am returning a Json object which i am getting with ajax call. i just want to show a alert box , if there is no data found between dates entered by user then a alert text should come on my jsp page.</p>
  
  <p>here is my JSP and servlet code iam using some method to do that but not getting success please any one guide me.</p>
  
  <p><strong>jsp (i am using JSP as a view)</strong></p>
  
  <pre><code>&lt;form id="formid" action="BarServlet" method="get"&gt;
      &lt;div class="container"&gt;
          &lt;h4&gt;Start Date:&lt;/h4&gt;
          &lt;input type="text" id="startdate" name="fromdate" width="276"
              placeholder="dd/mm/yyyy" required /&gt;
          &lt;h4&gt;End Date:&lt;/h4&gt;
          &lt;input type="text" id="enddate" name="todate" width="276"
              placeholder="dd/mm/yyyy" required /&gt;
  
  
          &lt;h4&gt;Outlets:&lt;/h4&gt;
          &lt;select id="all" name="outlet"&gt;
              &lt;option&gt;ALL&lt;/option&gt;
              &lt;c:forEach var="item" items="${obj.outlet}"&gt;
                  &lt;option&gt;${item}&lt;/option&gt;
              &lt;/c:forEach&gt;
          &lt;/select&gt;
          &lt;h5 class="NoDataFound"&gt;&lt;%=(request.getAttribute("NoDataFound") == null) ? "" : request.getAttribute("NoDataFound")%&gt;&lt;/h5&gt;
          &lt;div&gt;
              &lt;br&gt;
               &lt;button class="btn btn-default" type="submit"&gt;Search&lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>This is my servlet</strong></p>
  
  <p>protected void doGet(HttpServletRequest request, HttpServletResponse response)</p>
  
  <pre><code>        try {
              con = DBConnection.createConnection();
              statement = con.createStatement();
  
              ResultSet resultSet = statement.executeQuery(sql);
              if (!resultSet.isBeforeFirst() ) {    
                     request.setAttribute("NoDataFound", "NO Data Found"); 
                        RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp.jsp");
                        dispatcher.forward( request, response);
  
  
              }
              while (resultSet.next()) {
  
  
  &gt; if data is found them this will execute
  
                  list.add(map);
                  str = gson.toJson(list);
  
              }
  //System.out.println(list);
              response.setContentType("application/json");
  
              response.getWriter().write(str);
  
  
  
  }
  </code></pre>
  
  <p><strong>And here is my JavaScript file</strong></p>
  
  <pre><code>$(document).ready(
      function() {
          $("#formid").submit(
                  function(event) {
                      event.preventDefault(); // to stop form being submitted because it reloads the page.
                      $.ajax({
                          url : "BarServlet",
                          method : "GET",
                          data : {
                              fromdate : $("#startdate").val(),
                              todate : $("#enddate").val(),
                              outlet : $("#all").val()
                          },
                          success : function(data) {
                              $("#formid").hide(); // this will hide the form.
                              var paymentmode = [];
                              var amount = [];
                               console.log(data)
                              for ( var i in data) {
                                  paymentmode.push(data[i].CashamountX);
                                  amount.push(data[i].CashamountY);
  
                                  paymentmode.push(data[i].SwiggyAmtX);
                                  amount.push(data[i].SwiggyAmtY);
  
                                  paymentmode.push(data[i].KBAmtX);
                                  amount.push(data[i].KBAmtY);
  
                                  paymentmode.push(data[i].BigBasketAmtX);
                                  amount.push(data[i].BigBasketAmtY);
  
                                  paymentmode.push(data[i].ZomatoAmtX);
                                  amount.push(data[i].ZomatoAmtY);
  
                                  paymentmode.push(data[i].CreditCardAmtX);
                                  amount.push(data[i].CreditCardAmtY);
  
                                  paymentmode.push(data[i].CouponAmtX);
                                  amount.push(data[i].CouponAmtY);
  
                                  paymentmode.push(data[i].PayTMAmtX);
                                  amount.push(data[i].PayTMAmtY);
  
                                  paymentmode.push(data[i].CreditAmtX);
                                  amount.push(data[i].CreditAmtX);
                              }
                               Chart.defaults.global.legend.display = false;
                              var chartdata = {
                                  labels : paymentmode,
                                  datasets : [ {
  
                                      backgroundColor : [ "#87CEFA",
                                                          "#B0C4DE",
                                                          "#AFEEEE",
                                                          "#00BFFF",
                                                          "#87CEFA",
                                                          "#1E90FF",
                                                          "#708090",
                                                          "#778899",
                                                          "#696969",
                                                          "#808080" ],
                                                          borderColor : 'black',
                                      hoverBackgroundColor : '    #9370DB',
                                      hoverBorderColor : 'red',
                                      data : amount
                                  } ],
  
  
                              };
  
                              var ctx = $("#mycanvas");
  
                              var barGraph = new Chart(ctx, {
                                  type : 'bar',
                                  data : chartdata
                              });
                          }
                      });
                  });
      });
  </code></pre>