I have a java code that I want to change to javascript to add script in my html file. The fundamentals of my java code is to print out list of API names
public List<String> getApiNames() throws IOException{
String url = "https:give me api / // ";
HttpClient httpClient = new HttpClient(5,10000);
Response response = httpClient.callGetRequest(url, null, null);
String webPage = response.getResponseBody();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Map<String, Api> apis = mapper.readValue(webPage, new TypeReference<HashMap<String, Api>>(){});
List<String> list = new ArrayList<String>(apis.keySet());
return list;
}
and I want to add to my html that is formatted like this, list format
Is there anyway I can add the java code I have to html file?
I have a java code that I want to change to javascript to add script in my html file. The fundamentals of my java code is to print out list of API names
public List<String> getApiNames() throws IOException{
String url = "https:give me api / // ";
HttpClient httpClient = new HttpClient(5,10000);
Response response = httpClient.callGetRequest(url, null, null);
String webPage = response.getResponseBody();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Map<String, Api> apis = mapper.readValue(webPage, new TypeReference<HashMap<String, Api>>(){});
List<String> list = new ArrayList<String>(apis.keySet());
return list;
}
and I want to add to my html that is formatted like this, list format
Is there anyway I can add the java code I have to html file?
Share Improve this question asked Jul 13, 2016 at 1:17 Sherly KimSherly Kim 711 gold badge2 silver badges11 bronze badges 2- Based upon the image posted in your question, are you simply just wanting to view this data in your web page? is the data already being retrieved in a servlet or other webs service? – Scary Wombat Commented Jul 13, 2016 at 1:35
- Yes the data is already being retrieved. I want to show the list for the users to select the option – Sherly Kim Commented Jul 13, 2016 at 1:37
2 Answers
Reset to default 3In JavaScript, List<> is an array. Example ["one", "two", "three"]. List sizes do not have to defined up front in javascript and are dynamic. Below is an example using jquery's ajax to call a URL and get data from it:
http://api.jquery./jquery.ajax/
function getAPIArray(callback) {
var url = "https://someurl";
$.ajax({
type: 'GET',
url: url,
success: function (data) {
//data will be the response from the server (the api's)
// If it's not already in a list (array) then you have to build it up
//Example:
var array = [];
for (var i = 0; i < data.results.length; i++) {
array.push(data[i].apiName);
}
callback(array);
},
error: function (err) {
alert('error!');
console.log(err); //prints error object to console
}
});
}
function getsCalledWhenFinished(apiArray) {
//do whatever you want with the data..
}
getAPIArray(getsCalledWhenFinished); //invokes the ajax call
As per your further ments
Yes the data is already being retrieved. I want to show the list for the users to select the option
In you servlet you need to add the data to your request as an Attribute
request.setAttribute("tableData", data);
In your JSP you can use the c:forEach
tag to display the data
<%@ taglib uri="http://java.sun./jsp/jstl/core" prefix="c" %>
<c:forEach items="${tableData}" var="item">
${item}<br> <!-- Or put in to option/select -->
</c:forEach>