my dog learned polymorphism*
The moose likes Applets and the fly likes running applet through JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "running applet through JSP" Watch "running applet through JSP" New topic
Author

running applet through JSP

abhishek rawat
Greenhorn

Joined: Mar 25, 2011
Posts: 11

Hi,

I am creating pie chart using jfreechart , I am dynamicaly taking the values from DB and showing them in pie form. I have two java file one for database and other is for showing applet (applet class have main , which calls the DB class). All code properly works well in java. But I want these to be run through jsp. Could you please guide me how to move forward.

Thanks
Abhishek

Below is my applet .java file
-------------------------------------------------
package Pie_Chart;

import javax.swing.*;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;

public class PieChart extends ApplicationFrame
{

private Object chart;


/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
public PieChart(String title) {


super(title);

DB db = new DB();
db.dbConnect("jdbc:sqlserver://newinfomine:3468;instanceName=MSSQL2005", "eGActiveDB_762", "eGActiveDB_762" );
JPanel panel = new JPanel(new GridLayout(2, 2));
DefaultPieDataset dataset = new DefaultPieDataset();
DefaultPieDataset dataset1 = new DefaultPieDataset();
dataset.setValue("January "+DB.ravi_case, DB.ravi_case);
dataset.setValue("February "+DB.ravi_case_feb, DB.ravi_case_feb);
dataset.setValue("March "+DB.ravi_case_mar, DB.ravi_case_mar);

dataset1.setValue("January "+DB.pradeep_case, DB.pradeep_case);
dataset1.setValue("February "+DB.pradeep_case_feb, DB.pradeep_case_feb);
dataset1.setValue("March "+DB.pradeep_case_mar, DB.pradeep_case_mar);




JFreeChart chart1 = ChartFactory.createPieChart("Ravi Case", dataset, false, false, false);
JFreeChart chart2 = ChartFactory.createPieChart("Pradeep Case", dataset1, false, false, false);

panel.add(new ChartPanel(chart1));
panel.add(new ChartPanel(chart2));
panel.setPreferredSize(new Dimension(800, 600));
setContentPane(panel);



}

public static void main(String[] args)
{
PieChart demo = new PieChart("Pie Chart");
demo.setVisible(true);
}

}
Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
abhishek rawat wrote: But I want these to be run through jsp. Could you please guide me how to move forward.



What do you mean by "run through jsp"?
abhishek rawat
Greenhorn

Joined: Mar 25, 2011
Posts: 11

I want that if any third person wants to see this applet instead of running the whole java application. I give him a jsp url like http:\\localhost:9001\piechart.jsp and the applet automatically popup up in the browser showing the result to him. By doing this I don’t have to give him the access of the code and he will be able to see the pie chart (taking the values dynamically, every time anybody run the jsp).


abhishek rawat
Greenhorn

Joined: Mar 25, 2011
Posts: 11

I tried little more and created below jsp. but it is just keep on loading not showing any result... any help on optimizing this jsp Or let me know if this code is correct and it will show an applet on the browser.

<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import= "javax.swing.*" %>
<%@ page import= "java.awt.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="org.jfree.chart.*" %>
<%@ page import="org.jfree.chart.axis.*" %>
<%@ page import="org.jfree.chart.entity.*" %>
<%@ page import="org.jfree.chart.labels.*" %>
<%@ page import="org.jfree.chart.plot.*" %>
<%@ page import="org.jfree.chart.renderer.category.*" %>
<%@ page import="org.jfree.chart.urls.*" %>
<%@ page import="org.jfree.data.category.*" %>
<%@ page import="org.jfree.data.general.*" %>
<%@ page import= "org.jfree.chart.title.TextTitle" %>
<%@ page import= "org.jfree.data.general.DefaultPieDataset" %>
<%@ page import="org.jfree.ui.*" %>

<html>
<head>
</head>
<body>
<%
ResultSet rs = null;
Connection con =null;
Statement st=null;
try
{
String queryactive1 = null;
String queryactive2= null;
String queryactive3 = null;
String queryactive4 = null;
String queryactive5 = null;
String queryactive6 = null;
String queryactive7 = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con= DriverManager.getConnection("jdbc:sqlserver://newine:3468;instanceName=MSSQL2005","eGActiveDB_762","eGActiveDB_762");
st = con.createStatement();

%>
<p style="text-align:center"><B><u>Pie Chart Showing Case Count</u></B></p>
<%
queryactive1="SELECT count(a.case_id)as case_count , b.owner from EGPL_CASE a Right Outer Join (Select distinct OWNER from EGPL_CASE where owner in (1047,1213)) b on b.owner =a.owner and a.when_created BETWEEN '2011-01-01 00:00:00' AND '2011-01-01 1:59:59' group by b.OWNER order by b.owner";

rs=st.executeQuery(queryactive1);
rs.absolute(2);
int ravi_case=rs.getInt("case_count");
rs.absolute(4);
int pradeep_case=rs.getInt("case_count");



%>
<%

JPanel panel = new JPanel(new GridLayout(2, 2));
DefaultPieDataset dataset = new DefaultPieDataset();
DefaultPieDataset dataset1 = new DefaultPieDataset();
dataset.setValue("January "+ravi_case, ravi_case);

dataset1.setValue("January "+pradeep_case,pradeep_case);

JFreeChart chart1 = ChartFactory.createPieChart("Ravi Case", dataset, false, false, false);
JFreeChart chart2 = ChartFactory.createPieChart("Pradeep Case", dataset1, false, false, false);

panel.add(new ChartPanel(chart1));
panel.add(new ChartPanel(chart2));
panel.setPreferredSize(new Dimension(800, 600));


%>
<p style="text-align:center"><B><u>BAr Chart</u></B></p>


<%


}
catch(Exception e)
{
String error = e.toString();
e.printStackTrace();
%>

Error is : <%=error%>
<%
}
finally{
rs = null;
st = null;
con = null;
}
%>
</body>




</html>
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
If you intend this to be an applet, then what is all the Java code doing in the JSP? Start learning about applets here: http://download.oracle.com/javase/tutorial/deployment/applet/index.html. Once you have the applet running, you can deploy it from in an HTML or JSP page; that, too, is explained on that page.
abhishek rawat
Greenhorn

Joined: Mar 25, 2011
Posts: 11

Thanks for the reply!!!.... I first created the applet and it ran succesfuly. The issue is now I want to embedded applet in jsp . I know that we can call applet using this tab <applet code="MyApplet.class" width=100 height=140></applet> . BUt problem here is I have to run the applet from backend to keep updating this .class file (I think so) . But I want that it automatically gets updated when anybody calls this jsp using URL and he gets the latest data in applet form.


Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
What does "updating the class file" mean? If you have a new version of the applet with a new class file, just put it on the server, and it will be served to the browser next time someone tries to tun it.

And regardless of what you intend to do, that does not change the way applets work, no matter how much you want to. Applets run in the way described in the link I posted, or not at all. Embedding Java code in a JSP will cause that code to tun on the server, not on the client - meaning no client ever has a chance to see it. It sounds as if you do not yet have a good grasp of what applets and JSPs are, and how they work.
Haina Minawa
Ranch Hand

Joined: Oct 13, 2011
Posts: 119
abhishek rawat wrote:I tried little more and created below jsp. but it is just keep on loading not showing any result... any help on optimizing this jsp Or let me know if this code is correct and it will show an applet on the browser.

<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import= "javax.swing.*" %>
<%@ page import= "java.awt.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="org.jfree.chart.*" %>
<%@ page import="org.jfree.chart.axis.*" %>
<%@ page import="org.jfree.chart.entity.*" %>
<%@ page import="org.jfree.chart.labels.*" %>
<%@ page import="org.jfree.chart.plot.*" %>
<%@ page import="org.jfree.chart.renderer.category.*" %>
<%@ page import="org.jfree.chart.urls.*" %>
<%@ page import="org.jfree.data.category.*" %>
<%@ page import="org.jfree.data.general.*" %>
<%@ page import= "org.jfree.chart.title.TextTitle" %>
<%@ page import= "org.jfree.data.general.DefaultPieDataset" %>
<%@ page import="org.jfree.ui.*" %>

<html>
<head>
</head>
<body>
<%
ResultSet rs = null;
Connection con =null;
Statement st=null;
try
{
String queryactive1 = null;
String queryactive2= null;
String queryactive3 = null;
String queryactive4 = null;
String queryactive5 = null;
String queryactive6 = null;
String queryactive7 = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con= DriverManager.getConnection("jdbc:sqlserver://newine:3468;instanceName=MSSQL2005","eGActiveDB_762","eGActiveDB_762");
st = con.createStatement();

%>
<p style="text-align:center"><B><u>Pie Chart Showing Case Count</u></B></p>
<%
queryactive1="SELECT count(a.case_id)as case_count , b.owner from EGPL_CASE a Right Outer Join (Select distinct OWNER from EGPL_CASE where owner in (1047,1213)) b on b.owner =a.owner and a.when_created BETWEEN '2011-01-01 00:00:00' AND '2011-01-01 1:59:59' group by b.OWNER order by b.owner";

rs=st.executeQuery(queryactive1);
rs.absolute(2);
int ravi_case=rs.getInt("case_count");
rs.absolute(4);
int pradeep_case=rs.getInt("case_count");



%>
<%

JPanel panel = new JPanel(new GridLayout(2, 2));
DefaultPieDataset dataset = new DefaultPieDataset();
DefaultPieDataset dataset1 = new DefaultPieDataset();
dataset.setValue("January "+ravi_case, ravi_case);

dataset1.setValue("January "+pradeep_case,pradeep_case);

JFreeChart chart1 = ChartFactory.createPieChart("Ravi Case", dataset, false, false, false);
JFreeChart chart2 = ChartFactory.createPieChart("Pradeep Case", dataset1, false, false, false);

panel.add(new ChartPanel(chart1));
panel.add(new ChartPanel(chart2));
panel.setPreferredSize(new Dimension(800, 600));


%>
<p style="text-align:center"><B><u>BAr Chart</u></B></p>


<%


}
catch(Exception e)
{
String error = e.toString();
e.printStackTrace();
%>

Error is : <%=error%>
<%
}
finally{
rs = null;
st = null;
con = null;
}
%>
</body>




</html>



The code does not make sense at all. You should write database query and chart drawing in an applet, then use <applet> tag to embed the applet inside the JSP page.
You may also need to write a servlet that does the database queries and have the applet communicates with the servlet to retrieve data for the chart.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: running applet through JSP