aspose file tools*
The moose likes Struts and the fly likes JChart not being displayed on JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JChart not being displayed on JSP" Watch "JChart not being displayed on JSP" New topic
Author

JChart not being displayed on JSP

mani yas
Greenhorn

Joined: Aug 06, 2009
Posts: 5
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

JSP is a phenomenally poor choice of technology for this purpose. Why are you not doing this a servlet?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
mani yas
Greenhorn

Joined: Aug 06, 2009
Posts: 5
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
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
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.


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 06, 2009
Posts: 5
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 06, 2009
Posts: 5
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

Joined: Aug 06, 2009
Posts: 5
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JChart not being displayed on JSP