Hello fellows!
I have question.
Iam doing some project to my school and I have small problem.

I have chart (iam using jfreechart) in Chart.java file and after user is loading to his profe he should be able to push button and chart wit his statistics should appear. My problem is that I cant form proper onClick function. or mayby window.parent.location.href is wrong used?

<%@ page contentType="text/html; charset=iso-8859-1" language="java"%>
<html>
<head>

<script src="../WEB-INF/lib/amcharts.js" type="text/javascript"></script> 
<title>Successfully Login by JSP</title>

</head>

<body>

<input type=button onClick="window.parent.location.href = 'src/java/demo/Chart.java' " value='Chart'>


</body>
</html>

That is my web jsp web, just one button calling java chart..

And here is my java chart class, calling data from database (this class works fine when iam Runing it separately

package demo;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.jdbc.JDBCCategoryDataset;
import org.jfree.ui.*;

public class Chart
{
        public static void main(String[] args) throws Exception 
        {
                String query = "SELECT Category, value from pieData2";
                JDBCCategoryDataset dataset = new JDBCCategoryDataset(
                                "jdbc:mysql://localhost:3306/tabela", "com.mysql.jdbc.Driver",
                                "root", "root");

                dataset.executeQuery(query);
                JFreeChart chart = ChartFactory.createLineChart("Badanie Poziomu Cukru", "Numer Badania", "Poziom cukru",
                                dataset, PlotOrientation.VERTICAL, true, true, false);
                ChartPanel chartPanel = new ChartPanel(chart);
                chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
                ApplicationFrame f = new ApplicationFrame("Chart");
                f.setContentPane(chartPanel);
                f.pack();
                f.setVisible(true);
        }
}

And when I click Chart button iam getting In my browser

HTTP Status 404 -

type Status report

message

descriptionThe requested resource () is not available.

Cheers

Recommended Answers

All 6 Replies

Hi,
I hope you are creating a dynamic image using jfree chart and trying to display it on a jsp.

For this to happen "is your jsp should be hosted on server like tomcat?"
OR
"Are you running your code on a server ?"

If answer is no then you cannot include dynamically created image files in static HTML/jsp files.

If answer is yes then read below lines.

In chart class copy the code which is inside "public static void main" method into some other static method and that method should return you a string having the fully qulified path with name of the image produced by the method.
Then put this string into html img tag like this.

<img src="string returned by the method">

You cannot call the "src/java/demo/Chart.java" from a jsp as you have shown in your code.

Forgot to mention.

The method which produces the image should be called using a scriptlet from your jsp.

Thank you for your respond!

I have read about jfreecharts more, and i see there is more or less 2 ways of showing chart in website. As u said > producing image and returing in or using jfreecharts as a java applet? What do you think , with of those metods is better?

if you know applets very well ofcourse you can use jfree inside the java applet.

and I am sorry , I have not worked on applets, so I cannot comment on applets

I had used jfree library for one of my projects where i used servlets and jsps
There I called the classes similar to the code you have metioned above and these classes generated dynamic images and those images were displayed on jsps.

It was very easy to maintain. because it allowed us to use MVC disign pattern.

I was able to separate the business logic from presentation logic using servlet technology.

But lot of information is available to you on jfree charts , please google.
You can post any specific problem you are facing . I can always help

Hmm iam not sure if i good understound how kid of applets works - > first iam making some java fuction like this:

Following is the source code for the HelloWorld applet:


import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;

public class HelloWorld extends JApplet {
    //Called when this applet is loaded into the browser.
    public void init() {
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    JLabel lbl = new JLabel("Hello World");
                    add(lbl);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}

and than i have to compile it and save somehow as a jar?

and than use somethink like this in my web?

<script type="text/javascript">
//<![CDATA[

        var attributes = { code:'HelloWorld.class', archive:'examples/dist/applet_HelloWorld/applet_HelloWorld.jar',  width:150, height:30} ; 
        var parameters = {} ; 
        deployJava.runApplet(attributes, parameters, '1.4'); 
//]]>
</script

source : http://docs.oracle.com/javase/tutorial/deployment/applet/getStarted.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.