I have a java servlet that sends a json object to a jsp page in a javascript variabile with array format.
Here is my servlet ( a part of it):
List<HistoryLeavesScalar> returnedPastInfo = SaveDAO.getPastInformation(username);
JSONArray jsonArray = new JSONArray(returnedPastInfo);
String s = jsonArray.toString();
System.out.println("\n\n"+"JSON ARRAY is : "+s);
HttpSession session = request.getSession(true);
session.setAttribute("jsonArray",jsonArray);
this.getServletContext().getRequestDispatcher("/calendar.jsp")
.forward(request, response);
that system.out.print JSON is something like this in my console: [{"endDate":"2017-04-22","req":"2017-04-19","nr":2,"type":"CO","startDate":"2017-04-20","Dep":"2017-04-19"},{"endDate":"2017-04-22","req":"2017-04-20","nr":3,"type":"CM","startDate":"2017-04-20","Dep":"2017-04-19"}]
This Json Array I want to be visible in javascript tag like this in that format only: ( this is calendar.jsp - a part of it)
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="javax.servlet.jsp.jstl.core.*"%>
<%@ page import="javax.servlet.jsp.el.*" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<script type="text/javascript">
var USER_DAYS = [
<c:forEach items="${jsonArray}" var="jsonArray">
{
id: '${jsonArray.nr}',
date: '${jsonArray.req}',
title: '${jsonArray.type}',
startDate: '${jsonArray.startDate',
endDate: '${jsonArray.endDate',
allDay: true,
className: 'done'
}
</c:forEach>
];
</script>
I don't know how to access the values from that json that comes from the servlet in USER_DAYS variable (javascript). How to put the values from json in id, date, title, startDate, endDate.
I don't know if jstl works in a javascript tag. I don't know how to print that values ( whatever they are - it can contain many information, all in that format).
I want to mention that, if I change the javascript variable into somthing like this: it works just fine. But these are values which I have handwritten, but now I want them dynamically...and this information must come from servlet into calendar.jsp.
var USER_DAYS = [
{
id: 1,
date: '2017-04-05',
title: 'CO',
start: new Date(2017, 3, 5),
end: new Date(2017, 3, 7),
allDay: true,
className: 'done'
},
Can someone help me?




sin session and print it? It's already a correct string representation of the JSON object, no need to create loops and write broken JSONvar USER_DAYS = JSON.parse('${jsonArray}');