title: Access data after page load isn't successful
body: <p>I want to access data after page load, but it doesn't work. It seems not to call the getConnection function in jsp and it doesn't call response Json. I don't know why it doesn't work. What's wrong with my code and how to fix it? Thank you very much. </p>
  
  <p><strong>MyServlet.java</strong></p>
  
  <pre><code>import java.io.*;
  import java.util.ArrayList;
  import java.util.List;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import com.google.gson.Gson;
  import com.google.gson.JsonArray;
  import com.google.gson.JsonElement;
  import com.google.gson.reflect.TypeToken;
  
  /**
   * Servlet implementation class MyServlet
   */
  public class MyServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      /**
       * @see HttpServlet#HttpServlet()
       */
      public MyServlet() {
          super();
          // TODO Auto-generated constructor stub
      }
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          ArrayList&lt;Dish&gt; country=new ArrayList&lt;Dish&gt;();
          country=SQLConnection.getAllDish();
  
          Gson gson = new Gson();
          JsonElement element = gson.toJsonTree(country, new TypeToken&lt;List&lt;Dish&gt;&gt;() {}.getType());
  
          JsonArray jsonArray = element.getAsJsonArray();
          response.setContentType("application/json");
          response.getWriter().print(jsonArray);
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
      }
  
  }
  </code></pre>
  
  <p><strong>SQLConnection.java</strong></p>
  
  <pre><code>import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.sql.*;
  import java.util.ArrayList;
  import java.util.Properties;
  
  public class SQLConnection {    
  //  java.sql.Connection connection;
      private static Connection connection = null;
      static String url, driver, username, password;
  
      public static Connection getConnection() {
          if (connection != null)
              return connection;
          else {
              try {
                  Properties prop = new Properties();
                  InputStream inputStream = SQLConnection.class.getClassLoader().getResourceAsStream("/db.properties");
                  prop.load(inputStream);
                  driver = prop.getProperty("driver");
                  url = prop.getProperty("url");
                  username = prop.getProperty("user");
                  password = prop.getProperty("password");
                  Class.forName(driver);
                  connection = DriverManager.getConnection(url, username, password);
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              } catch (SQLException e) {
                  e.printStackTrace();
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return connection;
          }
  
      }
  
      public static ArrayList&lt;Dish&gt; getAllDish() {
          ArrayList&lt;Dish&gt; arrDish = null;
           try {
                  //Creating a statement object
                  Statement stmt = connection.createStatement();
  
                  //Executing the query and getting the result set
                  ResultSet rs = stmt.executeQuery("select * from dish");
                  arrDish = new ArrayList&lt;Dish&gt;();
                  //Iterating the resultset and printing the 3rd column
                  while (rs.next()) {
                      Dish item = new Dish(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getDouble(4), rs.getString(5),
                              rs.getString(6), rs.getString(7), rs.getString(8), rs.getInt(9), rs.getInt(10), rs.getInt(11), rs.getInt(12));
                      arrDish.add(item);                  
                  }
                  //close the resultset, statement and connection.
                  rs.close();
                  stmt.close();
                  connection.close();
                  return arrDish;
              } catch (SQLException e) {
                  e.printStackTrace();
              }
           return arrDish;
      }
  }
  </code></pre>
  
  <p><strong>hello.jsp</strong></p>
  
  <pre><code>   &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;link rel="stylesheet" href="${pageContext.request.contextPath}/style.css" /&gt;
  
      &lt;title&gt;&lt;fmt:message key="title" /&gt;&lt;/title&gt;
      &lt;/head&gt;
          &lt;body bgcolor="#FEDDFF"&gt;
              &lt;script
                  src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
              &lt;script type="text/javascript" src="js/slider.js"&gt;&lt;/script&gt;
  
              &lt;script&gt;
                  $(window).ready(function() {                
                          $.get('MyServlet',function(responseJson) {
                              if(responseJson!=null){
                                  $("#dishes").find("tr:gt(0)").remove();
                                  var table1 = $("#dishes");
                                  $.each(responseJson, function(key,value) { 
                                         var rowNew = $("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;");
                                         rowNew.children().eq(0).int(value['dishid']); 
                                         rowNew.children().eq(1).int(value['userid']); 
                                         rowNew.children().eq(2).text(value['dishName']); 
                                         rowNew.children().eq(3).double(value['numberOfPeople']); 
                                         rowNew.children().eq(4).text(value['dishImg']); 
                                         rowNew.children().eq(5).text(value['ingredient']);
                                         rowNew.children().eq(6).text(value['step']); 
                                         rowNew.children().eq(7).text(value['descOfDish']); 
                                         rowNew.children().eq(8).int(value['category1']); 
                                         rowNew.children().eq(9).int(value['category2']); 
                                         rowNew.children().eq(10).int(value['category3']); 
                                         rowNew.children().eq(11).int(value['rate']);
                                         rowNew.appendTo(table1);
                                  });
                              }
                          });
                      });
              &lt;/script&gt;
  
  &lt;div id="tabledish"&gt;
                              &lt;table cellspacing="0" id="dishes"&gt;
                                  &lt;tr&gt;
                                      &lt;th scope="col"&gt;dishid&lt;/th&gt;
                                      &lt;th scope="col"&gt;userid&lt;/th&gt;
                                      &lt;th scope="col"&gt;dishName&lt;/th&gt;
                                      &lt;th scope="col"&gt;numberOfPeople&lt;/th&gt;
                                      &lt;th scope="col"&gt;dishImg&lt;/th&gt;
                                      &lt;th scope="col"&gt;ingredient&lt;/th&gt;
                                      &lt;th scope="col"&gt;step&lt;/th&gt;
                                      &lt;th scope="col"&gt;descOfDish&lt;/th&gt;
                                      &lt;th scope="col"&gt;category1&lt;/th&gt;
                                      &lt;th scope="col"&gt;category2&lt;/th&gt;
                                      &lt;th scope="col"&gt;category3&lt;/th&gt;
                                      &lt;th scope="col"&gt;rate&lt;/th&gt;
                                  &lt;/tr&gt;
                              &lt;/table&gt;
                          &lt;/div&gt;
          &lt;/body&gt;
  </code></pre>