0

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 pictureenter image description here

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

manuel
  • 1,127
  • 1
  • 8
  • 15
  • 1
    *"This is the code for the JSP"* Ugh, that looks far from a true JSP file. It could as good be placed in a servlet class. Nonetheless, are you aware that you can pass around request parameters in URLs? See also a.o. http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page – BalusC Jul 07 '15 at 10:25
  • yes I know the code is for the jsp is ugly. – manuel Jul 07 '15 at 11:15
  • Do you want to have all the images displayed in the page as part of a single response? – ramp Jul 07 '15 at 11:38
  • yes that I was what I want – manuel Jul 07 '15 at 11:45
  • I added the key of the map to the url but know i get a nullpointerexception and i do not know why'");' – manuel Jul 07 '15 at 11:47
  • okay i solved the problem – manuel Jul 07 '15 at 12:59

0 Answers0