2

I have this code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter printWriter = response.getWriter();
    JSONArray arrayObj=new JSONArray();

    TimeTableDAO timeTable = new TimeTableDAO();
    ArrayList<String[]> data = timeTable.getTimeTableData();

    for (int i = 0; i < data.size(); i++){
        for (int j = 0; j < data.get(i).length; j++){
            printWriter.print("Token  " + data.get(i)[j] + "\n");

        }
    }

    response.sendRedirect("script.js");
    response.getWriter().write(data.toString());
}

Within my javascript file, once the page is loaded and everything is rendered it will fire this off:

$(window).on('load', function() {
$.get("TeacherServlet", function(response) {
    console.log(response);
});
});

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.

Please can someone help me do that. Thanks in advance :)

DwB
  • 37,124
  • 11
  • 56
  • 82
dark_illusion_909099
  • 1,067
  • 2
  • 21
  • 41

2 Answers2

2

So add everything to your JSONArray and then write that to the response

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter printWriter = response.getWriter();
    JSONArray arrayObj=new JSONArray();

    TimeTableDAO timeTable = new TimeTableDAO();
    ArrayList<String[]> data = timeTable.getTimeTableData();

    for (int i = 0; i < data.size(); i++){
        for (int j = 0; j < data.get(i).length; j++){
            arrayObj.put(data.get(i)[j]); // instead of writing directly, add it to the JSONArray.
        }
    }
    printWriter.println(arrayObj.toString()); //write the JSONArray to the response
    response.sendRedirect("script.js");
    //response.getWriter().write(data.toString()); //this doesn't do what you want


}
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

You have a number of issues:

  1. Add the data that you want to transfer from the Servlet to the destination page as a request attribute.
  2. Do not redirect to the js file. Instead, in the Servlet, get a RequestDispatcher from the request object and dispatch the request to the target resource (in your case, the js file).
  3. In the js file, you will need to retrieve the parameter values from the request.

Edit

Additional information.

  1. Do some reading about Java Servlet development. Consider a google search for "java servlet development tutorial".
  2. Bookmark the JEE JavaDoc and do some reading. HttpServletRequest is a good place to start.
  3. request.setAttribute(attributeName, attributeValue);
  4. request.getRequestDispatcher(stringPathToTargetResource);
  5. For the JS code, you are on your own. try a google search for "get request attribute with javascript". Here is a StackOverflow post that discusses this topic.
DwB
  • 37,124
  • 11
  • 56
  • 82