Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 9th, 2006, 11:02 PM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default call java file method to do something in JSP

i need to use jsp to call the method in java file.....i do a simple project for testing...i create a piechart using java code and the piechart save ask jpeg file....so i want using jsp to display the piechart in web browser.....any one can help me look over my coding here...thank a lot..

----------------------------------------------------------------------
StoreData.java
-----------------------------------------------------------------------
package com;

import java.io.File;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

import org.jfree.chart.*;
import org.jfree.data.general.*;


public class StoreData{

    private final static String filename = "C:\\CountryPieChart.yap";

    public static String getFname()
    {
        return "C:\\CountryPieChart.jpg";
    }

    public static void main(String[] args){
        //Delete the existing file
        new File(filename).delete();
        ObjectContainer db=Db4o.openFile(filename);
        try {
            StoreAllData();
            retrieveAllData();
        } finally{
            db.close(); //Close the database
        }
    }

    public static void StoreAllData() {
        //Delete the existing file
        new File(filename).delete();
        ObjectContainer db = Db4o.openFile(filename);

        //Add data to the database
        CountryPeople countryName_1 = new CountryPeople("Malaysia", 100);
        CountryPeople countryName_2 = new CountryPeople("New Z", 200);
        CountryPeople countryName_3 = new CountryPeople("UK", 300);
        CountryPeople countryName_4 = new CountryPeople("Thailand", 400);
        CountryPeople countryName_5 = new CountryPeople("Singapore", 50);

        //set the value to database
        db.set(countryName_1);
        db.set(countryName_2);
        db.set(countryName_3);
        db.set(countryName_4);
        db.set(countryName_5);
    }

    public static void retrieveAllData() {

        //Open db
        ObjectContainer db = Db4o.openFile(filename);

        //Retrieve via empty object
        CountryPeople cName = new CountryPeople(null, 0);

        ObjectSet result = db.get(cName);

        DefaultPieDataset dataset = new DefaultPieDataset();

        //retrieve the data from database
        while(result.hasNext()) {
            CountryPeople obj = (CountryPeople) result.next();
            dataset.setValue(obj.getName(), obj.getValue());
            //System.out.println(result.next());
        }

        //Create pie chart
        JFreeChart chart = ChartFactory.createPieChart(
                "Sample Chart",
                dataset,
                true,
                true,
                false);
        try {
            //save the pie chart as JPEG file
            ChartUtilities.saveChartAsJPEG(new File("C:\\CountryPieChart.jpg"), chart, 500, 300);
        } catch(Exception e) {
            System.out.println("Problem for creating chart");
        }
    }
}

-----------------------------------------------------------------------
DisplayWeb.jsp
---------------------------------------------------------------------
<%@page import org.jfree.data.general.PieDataset %>
<jsp:useBean id="myStoreData" class="com.StoreData" scope="page"/>

<html>
<head><title>Testing Displaying</title>
</head>
<body>

<b>Testing Display Page</b>

<img src="<jsp:getProperty name="myStoreData" property="fname" />" />
</body>

</html>
---------------------------------------------------------------------
error display in browser is
err - DisplayWeb.jsp(1,16) equal symbol expected


 
Old October 11th, 2006, 01:43 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A proper solution would be to change your StoreData class into a HttpServlet. E.g. "StoreData extends javax.servlet.http.HttpServlet". Then convert your main() method to doGet(ServletRequest, ServletReponse). With the response parameter, you can get an OutputStream to write the image to. So pass the request parameter to retrieveAllData(), and instead of calling ChartUtilities.saveChartAsJPEG to output the JPEG, call ChartUtilities.writeChartAsJPEG(response.getOutput Stream(), ...). Map StoreData to a URL in web.xml, and then your JSP can link directly to that servlet mapping in the <img src="">.

Jon Emerson
http://www.jonemerson.net/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a function on every method call Takashi321 General .NET 1 September 21st, 2007 09:33 AM
Can I call a method that populate a Label??? cp75 ASP.NET 1.0 and 1.1 Basics 2 January 12th, 2007 05:39 AM
pass variable from jsp to java file ypsmag JSP Basics 0 November 1st, 2006 04:22 AM
How to call a method on a remote object eloyitomio C# 1 July 21st, 2006 10:17 AM
How to call a C# method from VB.Net? aliarifpk General .NET 1 February 26th, 2004 04:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.