0

I've builded a servlet which make some Query to a database pick some information up and them forward it to the jsp page.

Apart of this information, now I wanted to include, as a request, a chart which has been created in the servlet.

Servlet code:

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

 . More Servlet code..
 .
 .
 . 
 case "Charts": {

            String QueryStatus = "select distinct NodeStatus from nodes;";

            Operations oper = new Operations();


            ArrayList<?> ListStatus = oper.getList(QueryStatus);              



            request.setAttribute("ListStatus", ListStatus);

Till here everything works well, but once i add the following code:

            JFreeChart Chart = null;
            BACENGQueryDatabases DataChart = new BACENGQueryDatabases();
            Chart = DataChart.GetChart("select Name, StNode from controls;");

            if (Chart != null) {
                int width = 500;
                int height = 350;
                final ChartRenderingInfo info = new ChartRenderingInfo(new    StandardEntityCollection());
                response.setContentType("image/png");
                OutputStream out = response.getOutputStream();
                ChartUtilities.writeChartAsPNG(out, Chart, width, height, info);
            }

The chart is printed but nothing else. The RequestDispatcher is truncate.

          RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/Charts.jsp"); //Set page to redirect data
            rd.forward(request, response);// Forward data to the next page
            break;
        }

Is there any way to forward the chart, like I'm doing with ( request.setAttribute("ListStatus", ListStatus);) to be used on the same jsp page.

I refuse to use print the chart to a file and then use it on jsp because the I/O performance, since many charts will be created.

Sallyerik
  • 489
  • 2
  • 11
  • 24

1 Answers1

2

A single response can only have one content type. It is either a normal HTML page (type = text/html) or a chart (type = image/png) but not both.

As you forward to a JSP, I presume you want to build an HTML page containing a chart (in fact a png image). You include the image in the HTML file with an <img> tag. At this point you have 2 possibilities :

  • a simple and classical <img> where the source is an URL. The browser will issue a second request at the url you give and then you send directly the image. Simple and robust, but needs 2 requests.
  • a base 64 embedded image. The srcattribute contains the base64 encoded image, something like

    src="data:image/png;base64,__base64 encoded image__"
    

    You save one request, but if your image is large, the encoding will give a an increase in size between 30% and 40%. And it is not always correctly understood by all browsers. You should look at this other post Embedding Base64 Images for more details.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Great!!! finally I've decided to indicate the Url and it works as I wanted. I was trying with [] but it got into a loop asking to login all the time, I don't know way.. Thanks. – Sallyerik Oct 22 '14 at 07:21