Dynamically generate JFreeChart in servlet
Asked Answered
L

2

5

I'm trying to generate graphs dynamically using JFreeChart as a result of some checkboxes the user selects, but I can't figure out how best to get the generated datasets into chart form (I have code that makes charts from these, but need to produce pngs) and into the JSP view. Currently, I can only think of sending the Datasets to the JSP, but can't think of what to do from there... How do I make it so that: user submits form to servlet, servlet generates datasets, charts produced from datasets, pngs from charts and finally pngs dispatched to jsp? Or something along those lines.

public void doPost(HttpServletRequest request,
                   HttpServletResponse response)
                    throws IOException, ServletException{

    String[] metrics     = request.getParameterValues("metrics");
    String[] fileNames   = request.getParameterValues("files");

    List<CategoryDataset> results = new ArrayList<CategoryDataset>();
    DMCalc calculator = new DMCalc(metrics, fileNames);  
    calculator.calculateResults();
    results.add(calculator.getEditDistanceDataset());
    results.add(calculator.getSimilarityDataset());
    results.add(calculator.getTimeChartDataset());

    request.setAttribute("results", results);
    RequestDispatcher view = request.getRequestDispatcher("metricResult.jsp");

    view.forward(request, response);
}

UPDATE:

By having the doPost method generate the datasets from the user post, they can then be stored in fields, subsequently the RequestDispatcher forwards the user to the JSP which then calls the servlet's doGet method in an img tag, which uses the datasets stored earlier in the fields to produce a png and that is then displayed by the HTML in the JSP.

Libel answered 10/8, 2009 at 15:55 Comment(3)
Sounds like your solution will break as soon as two users are using your page at the same time -- it is not safe to store anything in the fields of a servlet class!Pants
ah, ok - is there a way to achieve the same thing?Libel
The easy solution is the HttpSession object. Do HttpSession session = request.getSession(); and then session.setAttribute("results", results); in the form servlet, and session.getAttribute("results") in the chart servlet.Pants
P
4

Have your JSP file include an tag where the src attribute is the name of your servlet. Then, you simply have the servlet return the PNG chart:

    OutputStream out = response.getOutputStream();
    response.setContentType("image/png");
    ChartUtilities.writeChartAsPNG(out, chart, width, height);

JSP pages are really only intended to output HTML or other text data. Although you could force the JSP to output the PNG, there is no benefit to doing it that way.

It sounds like you want to create a dynamic page that updates based on a drop-down menu state change. For this, you need to use Javascript that triggers when the menu changes, and updates the value of the img tag's src attribute. Then the browser will reload the image from your servlet with a new chart.

Pants answered 10/8, 2009 at 16:15 Comment(4)
How do I make it so that: user submits form to servlet, servlet generates datasets, charts produced from datasets, pngs from charts and finally pngs dispatched to jsp?Libel
This servlet only does one thing -- creates the PNG image of a chart. You will still need to have a separate JSP page to hold the <img> tag. There is no RequestDispatcher needed for this.Pants
let me phrase it differently - I cannot display the pngs without the datasets, which has to be generated by the user submitted form. I could make a servlet that only handles one thing, producing a PNG, but how would I give it the dataset to be able to do that, since it is generated by the form submitted?Libel
Your basic problem here is that of saving state across two HTTP requests. Your form will return HTML to the browser -- one HTTP request. This HTML will contain an <img> tag that causes a second HTTP request for the chart. One way to solve the problem is with the HttpSession object. Whatever you store in it will be available to the servlet that creates the chart. Another way to solve it is by passing query parameters, e.g. <img src=chartServlet?metrics=1,2,3&files=one,two,three">.Pants
M
5

I would suggest that you use the ServletUtilities class. It saves in the java tempdir AND cleans up when the session is invalidated. :) Another hint for then displaying the file is to use the DisplayChart servlet to get your images. This goes in web.xml

      <servlet>
    <servlet-name>DisplayChart</servlet-name>
    <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
  </servlet>
   <servlet-mapping>
        <servlet-name>DisplayChart</servlet-name>
        <url-pattern>/servlet/DisplayChart</url-pattern>
    </servlet-mapping>

This is then how you display the image using jstl:

<img src="<c:url value='/servlet/DisplayChart?'><c:param name='filename' value='${yourFileNameHERE}' /></c:url>" alt=""/>
Matter answered 30/11, 2009 at 18:20 Comment(0)
P
4

Have your JSP file include an tag where the src attribute is the name of your servlet. Then, you simply have the servlet return the PNG chart:

    OutputStream out = response.getOutputStream();
    response.setContentType("image/png");
    ChartUtilities.writeChartAsPNG(out, chart, width, height);

JSP pages are really only intended to output HTML or other text data. Although you could force the JSP to output the PNG, there is no benefit to doing it that way.

It sounds like you want to create a dynamic page that updates based on a drop-down menu state change. For this, you need to use Javascript that triggers when the menu changes, and updates the value of the img tag's src attribute. Then the browser will reload the image from your servlet with a new chart.

Pants answered 10/8, 2009 at 16:15 Comment(4)
How do I make it so that: user submits form to servlet, servlet generates datasets, charts produced from datasets, pngs from charts and finally pngs dispatched to jsp?Libel
This servlet only does one thing -- creates the PNG image of a chart. You will still need to have a separate JSP page to hold the <img> tag. There is no RequestDispatcher needed for this.Pants
let me phrase it differently - I cannot display the pngs without the datasets, which has to be generated by the user submitted form. I could make a servlet that only handles one thing, producing a PNG, but how would I give it the dataset to be able to do that, since it is generated by the form submitted?Libel
Your basic problem here is that of saving state across two HTTP requests. Your form will return HTML to the browser -- one HTTP request. This HTML will contain an <img> tag that causes a second HTTP request for the chart. One way to solve the problem is with the HttpSession object. Whatever you store in it will be available to the servlet that creates the chart. Another way to solve it is by passing query parameters, e.g. <img src=chartServlet?metrics=1,2,3&files=one,two,three">.Pants

© 2022 - 2024 — McMap. All rights reserved.