I am writing an application to display charts on JSP using JChart API.
I am using Struts2 to do the same.
Following is the code in JSP, which displays Chart ,when called directly from browser (i.e. if I call this JSP directly
from url http://localhost:8000/test.jsp).
But when it is called from an action class and forwarded to this JSP, I get an error "getOutputStream() has already been called for this response" on server console and nothing is being displayed on JSP.
Here is the JSP, I have written
How can I avoid getting this error, so that I can see Chart on JSP when called from an action in struts2.
Any help will be highly appreciated.
In a JSP, by default any character outside of Java tags is sent directly to the JSP's output stream. In this case, there are two enters at lines 5 and 6. Either turn on output buffering (Google around a bit) or simply remove those enters.
Thanks for your responses,
Removing these enters(at line 5 and 6) doesn't make any difference, and still getting same error.
As I am using struts2 frame work for this purpose, hence I have left no other choice then using this in JSP not in servlet.
If I write it in Action class, it still forwards it to a JSP. is there a way to display it in JSP, if it is written in action class?
While I'm not too familiar with Struts, streaming images from JSPs just doesn't work as Bear says. For embedding charts in Java web apps I recommend a tag library such as cewolf (see link in my signature) - it also uses JFreeChart, so the look and the capabilities will be similar to what you're doing now.
Appreciate all the responses. But still didn't find any solution for this.
Do we have any other way to get around this (Using JChart)?
Can we do it in action class and send response to JSP to display the chart? If yes, can someone share the example?
Or Can we do something in JSP to eleminate this error (and Chart will be displayed)?
As I reported, JSP works fine if it is called directly from browser, but it doesn't work (and throws exception) if forwarded from an action in struts2.
Thanking all for your responses.
Thank you very much Dave, your suggestion worked.
If I call the action from browser like http://localhost:8080/TestApplication/ShowChart.action (Where ShowChart action has all the code of JSP), and forward to null. It works fine.
But I have another situation, when it doesn't work.
If I call a JSP (Jsp Code given below) from browser (using http://localhost:8080/TestApplication/testChart.jsp), which internally calls this action as described below, it doesn't work. and gives me error Error loading '/TestApplication/ShowChart.action' (200 OK) . Though it doesn't throw any exception on console.
How can I avoid above highlighted error (error loading 200 OK) ?
I really appreciate all your responses.
Many thanks in advance.