01 /*
02 * Created on Sept 21, 2007
03 *
04 */
05 package com.x8ing.mc.web;
06
07 import java.io.IOException;
08
09 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.jfree.chart.ChartFactory;
15 import org.jfree.chart.ChartUtilities;
16 import org.jfree.chart.JFreeChart;
17 import org.jfree.chart.plot.PlotOrientation;
18 import org.jfree.data.statistics.HistogramDataset;
19
20 import com.x8ing.mc.MonteCarloControler;
21
22 /**
23 * Servlet for executing the MonteCarlo simulation process.
24 *
25 * @author Patrick Heusser
26 */
27 public class HistogramImageServlet extends HttpServlet {
28
29 private static final long serialVersionUID = 1L;
30
31 public HistogramImageServlet() {
32 super();
33 }
34
35 protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
36 doPost(arg0, arg1);
37 }
38
39 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40
41 setNonCachingHeaders(response);
42
43 MonteCarloControler mcc = (MonteCarloControler) request.getSession().getAttribute(
44 ProcessMCAction.SESSION_KEY_MONTE_CARLO_CONTRLOER);
45
46 HistogramDataset data = new HistogramDataset();
47 data.addSeries("occurence", mcc.getBalanceValues(), 100);
48
49 JFreeChart chart = ChartFactory.createHistogram("Earnings Histogram", "earning", "number of occurrences", data,
50 PlotOrientation.VERTICAL, false, false, false);
51
52 try {
53
54 ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 800, 600);
55
56 } catch (IOException e) {
57 System.err.println("Problem occurred creating chart.");
58 e.printStackTrace();
59 response.getOutputStream().print(e.toString());
60
61 }
62
63 }
64
65 private void setNonCachingHeaders(HttpServletResponse response) {
66
67 response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");
68
69 // Set standard HTTP/1.1 no-cache headers.
70 response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
71
72 // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
73 response.addHeader("Cache-Control", "post-check=0, pre-check=0");
74
75 // Set standard HTTP/1.0 no-cache header.
76 response.setHeader("Pragma", "no-cache");
77 }
78
79 }
|