Hitachi Vantara Pentaho Community Forums
Results 1 to 2 of 2

Thread: Newbie question about DisplayServlet

  1. #1
    Join Date
    Mar 2003
    Posts
    4,947

    Default Newbie question about DisplayServlet

    I am trying to have a servlet to generate a chart into a file in temp directory and have DisplayServlet to display it in the JSP page using
    <img src. > tag but keep getting this error:

    Here is the code snipet:
    Servlet Code:

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    String filename;
    HttpSession session = request.getSession();

    OutputStream out = response.getOutputStream();
    PrintWriter pw = new PrintWriter(out);

    try {
    String type = request.getParameter("type");

    JFreeChart chart = null;
    if (type.equals("pie")) {
    chart = createPieChart();
    }
    else if (type.equals("bar")) {
    chart = createBarChart();
    }

    if (chart != null) {

    ChartRenderingInfo info =
    new ChartRenderingInfo(new StandardEntityCollection());
    filename = ServletUtilities.saveChartAsPNG(chart, 400, 300, info, session);

    // Write the image map to the PrintWriter
    ChartUtilities.writeImageMap(pw, filename, info);
    pw.flush();

    String graphURL = request.getContextPath()
    + "/servlet/DisplayChart?filename=" + filename;

    System.err.println("graphURL=" + graphURL);

    request.setAttribute("graphURL", graphURL);
    request.setAttribute("filename", filename );
    }
    }
    catch (Exception e) {
    System.err.println(e.toString());
    }


    RequestDispatcher rd = request.getRequestDispatcher("/DisplayChart.jsp");
    rd.forward(request, response);

    }

    DisplayChart.jsp:
    --------------------
    <html>

    <% String graphURL = (String) request.getAttribute("graphURL");
    String filename = (String) request.getAttribute("filename");
    %>

    <body>





    [img]<%= graphURL %>[/url]">

    </body>
    </html>
    =============================
    I already define properly Servlet name/mapping in my web.xml

    Here is the error displayed on the browser (DisplayChart.jsp):
    =========================================
    <MAP NAME="jfreechart-9300.png">
    <AREA SHAPE="POLY" COORDS="200,63,211,63,223,66,234,69,244,75,254,81,262,89,270,98,277,108,282,118,285,129,288,141,288,152,287,164,285,175,281,186,276,197,200,151,200,151" title="One = 43.2">
    <AREA SHAPE="POLY" COORDS="276,197,262,214,245,227,200,151,200,151" title="Two = 10">
    <AREA SHAPE="POLY" COORDS="245,227,232,234,217,238,203,240,188,239,174,236,160,231,147,223,136,213,200,151,200,151" title="Three = 27.5">
    <AREA SHAPE="POLY" COORDS="136,213,124,198,116,181,112,163,111,144,200,151,200,151" title="Four = 17.5">
    <AREA SHAPE="POLY" COORDS="111,144,113,132,116,120,121,110,127,100,200,151,200,151" title="Five = 11">
    <AREA SHAPE="POLY" COORDS="127,100,142,84,159,72,179,65,200,63,200,151,200,151" title="Six = 19.4">
    </MAP>
    <PRE>java.lang.IllegalStateException: Response has already been committed

    void com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.http.EvermindHttpServletResponse.resetBuffer()

    EvermindHttpServletResponse.java:1847

    void com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.http.ServletRequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

    ServletRequestDispatcher.java:200

    void chart.Servlet2.doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

    Servlet2.java:104

    void javax.servlet.http.HttpServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

    HttpServlet.java:760

    void javax.servlet.http.HttpServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

    HttpServlet.java:853

    void com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.http.ServletRequestDispatcher.invoke(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

    ServletRequestDispatcher.java:721

    void com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.http.ServletRequestDispatcher.forwardInternal(javax.servlet.ServletRequest, javax.servlet.http.HttpServletResponse)

    ServletRequestDispatcher.java:306

    boolean com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.http.HttpRequestHandler.processRequest(com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.ApplicationServerThread, com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.http.EvermindHttpServletRequest, com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.http.EvermindHttpServletResponse, java.io.InputStream, java.io.OutputStream, boolean)

    HttpRequestHandler.java:767

    void com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.http.HttpRequestHandler.run(java.lang.Thread)

    HttpRequestHandler.java:259

    void com.evermind[Oracle9iAS (9.0.3.0.0) Containers for J2EE].server.http.HttpRequestHandler.run()

    HttpRequestHandler.java:106

    void EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run()

    PooledExecutor.java:803

    void java.lang.Thread.run()

    Thread.java:484

    </PRE></BODY></HTML>


    Any thoughts? Any helps are fully appreciated!!

  2. #2
    Join Date
    Mar 2003
    Posts
    8,085

    Default

    Hi,

    AFAIK, the HTTP header must be set before the content is created, so defining HTTP headers after some of the content was written is no good idea ...

    You write the ImageMap for the chart into the output stream (this will lock the response object, so that no http header can be modified anymore).

    Then you forward the request to the JSP page, which will try to continue writing (and could try to change the HTTP-Response headers as well)... I guess this is the point where it crashes ...

    Try to write the imagemap into a string and add that string as attribute to the request object before forwarding it to the JSP. Then you can write the imagemap within the JSP, and the thing should not crash anymore ...

    Have more fun,
    said Thomas

    Btw: this is JFreeReport, JFreeChart related topics can be found in the other forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.