简体   繁体   中英

How can i parse JsonArray from jsp to Javascript

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...

One suggestion might be to attach a hidden dom element to a javascript variable, like ap, 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM