title: How to go about passing the data from Java Servlet to JavaScript
body: <p>I have this code: </p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      PrintWriter printWriter = response.getWriter();
      JSONArray arrayObj=new JSONArray();
  
      TimeTableDAO timeTable = new TimeTableDAO();
      ArrayList&lt;String[]&gt; data = timeTable.getTimeTableData();
  
      for (int i = 0; i &lt; data.size(); i++){
          for (int j = 0; j &lt; data.get(i).length; j++){
              printWriter.print("Token  " + data.get(i)[j] + "\n");
  
          }
      }
  
      response.sendRedirect("script.js");
      response.getWriter().write(data.toString());
  }
  </code></pre>
  
  <p>Within my javascript file,
  once the page is loaded and everything is rendered it will fire this off:</p>
  
  <pre><code>$(window).on('load', function() {
  $.get("TeacherServlet", function(response) {
      console.log(response);
  });
  });
  </code></pre>
  
  <p>I am trying to pass the data that I have in my servlet back to javascript so that I can do other things to the data using Jquery.
  However I want this data to be passed back as a JSON format. </p>
  
  <p>Please can someone help me do that. Thanks in advance :)</p>