This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JChart not being displayed on JSP

 
mani yas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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.

Thanks in advance.
Mani.

 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's just move this to the JSP forum

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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP is a phenomenally poor choice of technology for this purpose. Why are you not doing this a servlet?
 
mani yas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Appreciate your help.

Thanks,
Mani
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mani yas wrote: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.

I cannot believe that Struts forces you to use a JSP for this.

Moved to the Struts forum for further discussion.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S2 actions can write their own response and indicate they've done so by returning null or ActionSupport.NONE (IIRC).

That said, yuck; not sure I'd do it this way. I'd most likely write a custom result type, which would be *substantially* cleaner, and could then be released into the wild for others to use.

There's also an existing JFreeChart result type already if writing a new result type isn't something you're interested in doing.
 
mani yas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Mani

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, if you do all that work in the action and return null or ActionSupport.NONE you don't have to use a JSP.
 
mani yas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

testChart.jsp



How can I avoid above highlighted error (error loading 200 OK) ?
I really appreciate all your responses.
Many thanks in advance.

Mani.


 
mani yas
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Does anyone have any suggestion / comment / solution for this issue (Where I am getting
Error loading '/TestApplication/ShowChart.action' (200 OK)
error on JSP) as described above?

Thanks in advance.

Mani
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic