1

Here is my code :

Display.jsp:

    <%
    JSONArray jsonArray = new JSONArray();
    String location = request.getParameter("location");
    Class.forName(OracleDriver.class.getName().toString());
    Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","ram","ram");
    PreparedStatement preparedStatement = connection.prepareStatement("select * from job where location = ?");
    preparedStatement.setString(1,location);
    ResultSet resultSet = preparedStatement.executeQuery();
    int index = 0;
    if(!resultSet.next()){
            jsonArray.add(0, new Job("NA","NA","NA","NA","NA"));
            out.println(jsonArray);
    }else{
        while(resultSet.next()){
            index ++;
            Job job = new Job(resultSet.getString("title"),resultSet.getString("location"),resultSet.getString("eligibility"),resultSet.getString("position"),resultSet.getString("pdate"));
            jsonArray.add(index, job);
        }
        out.println(jsonArray);
    }

%>

jsonArray containing list of Job objects, How can i parse jsonArray in javascript

Any ideas...

1
  • 1
    use jQuery.getJSON( ) also set content type in jsp page response.setContentType("application/json"); Commented Oct 26, 2014 at 5:04

1 Answer 1

2

One suggestion might be to attach a hidden dom element to a javascript variable, like a p, or hidden input.

Then, set the inner html to your json array

<p id="myJsonArray" style="display:none;"> <%= jsonArray %> </p>

Then finally, attach some event, to trigger a javascript function to read the inner html of that

... If you want it automatic, maybe a timed interval or something to check if that element changed.

I would honestly forget all of this though, and just write a web service (use jersey or something), and create an ajax call to get what you want :) But, that's my opinion.

Let me know if you want any further ideas!

Here's some working code. I would still like to emphasise that this is likely not the best approach. Having this much code mixed within a html page is very hard to maintain, and is not readable. I found this linked to on another StackOverflow post. It's worth a read. https://netbeans.org/kb/docs/javaee/ecommerce/design.html#architecture
The StackOverflow post: JSF vs Facelets vs JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="java.util.List,java.util.ArrayList,org.json.JSONArray,java.util.Arrays" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>test</title>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.3/css/jquery.dataTables.css">

<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>

<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.3/js/jquery.dataTables.js"></script>

<script>
$(function(){
    //If your data won't change, you could just draw the table immediately after the page loads.
    var testTable = $("#dynamicTable").DataTable({
        data : JSON.parse($("#hiddenJson").html())
    });

    //If you're going to be updating data dynamically, you might 
    $("#refresh").click(function(){
        var data = JSON.parse($("#hiddenJson").html());

        testTable.clear();
        var modifiedData;
        for(r in data) {
            modifiedData = data[r];
            modifiedData[0] += " dynamically added";
            modifiedData[1] += " dynamically added";
            modifiedData[2] += " dynamically added";
            testTable.row.add(modifiedData);        
        };              
        testTable.draw();
    });
});

</script>
</head>
<body>
<%

    JSONArray jsonArray = new JSONArray();
    String location = request.getParameter("location");
   //  Class.forName(OracleDriver.class.getName().toString());
   // Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","ram","ram");
   // PreparedStatement preparedStatement = connection.prepareStatement("select * from job where location = ?");
   // preparedStatement.setString(1,location);
   // ResultSet resultSet = preparedStatement.executeQuery();
    //int index = 0;
    //if(!resultSet.next()){
    //        jsonArray.add(0, new Job("NA","NA","NA","NA","NA"));
    //        out.println(jsonArray);
    //}else{
    //    while(resultSet.next()){
    //        index ++;
    //        Job job = new Job(resultSet.getString("title"),resultSet.getString("location"),resultSet.getString("eligibility"),resultSet.getString("position"),resultSet.getString("pdate"));
    //        jsonArray.add(index, job);
    // }    
    jsonArray.put(0, ("NA,NA,NA").split(",")); 
    jsonArray.put(1, ("NA2,NA2,NA2").split(",")); 
    jsonArray.put(2, ("NA3,NA3,NA3").split(",")); 

%>

<p id="hiddenJson" style="display:none;"> <%= jsonArray.toString() %> </p>

<table id="dynamicTable">
  <thead>
    <th>column 1</th>
    <th>column 2</th>
    <th>column 3</th>
  </thead>
</table>

<button id="refresh"> Update Data </button>
</body>
</html>
Sign up to request clarification or add additional context in comments.

5 Comments

Thank for reply...But here i'm not going to display jsonArray directly.i put all those objects hold by jsonarray in a table
Right, the hidden element only holds the data, and is not displayed directly. Use javascript to read in the elements data as json, then do what you want with it...
Can u please provide some skelton code ...i don't have an idea
I will do so tomorrow if someone else doesn't by then :) In the mean time, maybe try creating a button that reads the innerHTML of an element.
updated the posting with working code, and copied some directly from the jQuery DataTables examples page to add just a little extra something. I don't have access to your database and other objects such as Job....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.