I have a problem understanding the connection of JSP and Servlet. I have a JSP which should display a table with the content of a map and the corresponding picture
The Table shows how it would look like. But every time i call the servlet in the loop i will get only the image of the last entry of the map. I know that I have to show the Servlet through a Query-String which Image belongs to which URL but I do not know how do this.
This is the code for the JSP: it gets the Data form a Java-class, no rocket-Science
Map<String, HashMap<String, ArrayList<MethodLogEntryObject>>> mainMap = rS.readMap();
out.println("<h3>Data-Visualization</h3>");
out.println("<table border=\"1\" style=\"float:left\">");
for(Entry<String, HashMap<String, ArrayList<MethodLogEntryObject>>> entry : mainMap.entrySet()){
session.setAttribute("entryVisualize", entry);
out.println("<tr><td>");
out.println("<b>TraceID: </b>"+entry.getKey()+"<br>");
HashMap<String, ArrayList<MethodLogEntryObject>> entry2 = entry.getValue();
for(String key2 : entry2.keySet()){
out.println("<b>Application: </b>"+key2+"<br>");
ArrayList<MethodLogEntryObject> list = entry2.get(key2);
Collections.sort(list);
for(int i=0; i<list.size(); i++){
out.println("<b>Method: </b>"+list.get(i).getMethodName()+" <b>Time: </b>"+list.get(i).getProcTime()+"<br>");
}
}
out.println("</td>");
out.println("<td><img src=\"/relationVisualization\"></td>");
out.println("</tr>");
}
out.println("</table>");
For every Entry in the main Map a Servlet is called to display the Data in Form of chart. The code of the Servlet:
HttpSession session = request.getSession(false);
response.setContentType("image/png");
OutputStream out = response.getOutputStream();
Entry<String, HashMap<String, ArrayList<MethodLogEntryObject>>> entryVis = (Entry<String, HashMap<String, ArrayList<MethodLogEntryObject>>>) session.getAttribute("entryVisualize");
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
HashMap<String, ArrayList<MethodLogEntryObject>> entry2 = entryVis.getValue();
for(String key2 : entry2.keySet()){
ArrayList<MethodLogEntryObject> list = entry2.get(key2);
Collections.sort(list);
int sumProc= 0;
for(int i=0; i<list.size(); i++){
if(i==0){
dataset.addValue(list.get(i).getProcTime(), key2, list.get(i).getMethodName());
}else{
sumProc+=list.get(i).getProcTime();
}
}
if(sumProc>0){
dataset.addValue(sumProc, key2, list.get(1).getMethodName());
}
}
JFreeChart chart = ChartFactory.createBarChart(entryVis.getKey(), "Methods", "ProcTime", dataset, PlotOrientation.VERTICAL, true, true, false);
chart.setBorderPaint(Color.gray);
chart.setBorderStroke(new BasicStroke(2.0f));
chart.setBorderVisible(true);
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
out.flush();
out.close();
Could someone explain how to get the corresponding image to each Entry?
Manuel