求救,解决一个程序问题?????(JSP调用JavaBean在网页上动态生成柱状图 )谢谢!请进_____
JSP调用JavaBean在网页上动态生成柱状图
第一步:创建一个Java Bean用来生成jpg文件 源程序如下:
package draw_test1;
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import javax.servlet.*;
//生成图片的 Java Bean
public class ChartGraphics {
BufferedImage image;
ChartGraphics() { }
public void createImage(String fileLocation){
try{
FileOutputStream fos = new FileOutputStream(fileLocation);
BufferedOutputStream bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
}catch(Exception e){
System.out.println(e);
}}
public void graphicsGeneration(int h1,int h2,int h3,int h4,int h5){
final int X=10;
int imageWidth=300;//图片的宽度
int imageHeight=300;//图片的高度
int columnWidth=30;//柱的宽度
int columnHeight = 200;//柱的最大高度
try{
ChartGraphics chartGraphics = new ChartGraphics();
chartGraphics.image = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_RGB);
Graphics graphics = chartGraphics.image.getGraphics();
graphics.setColor(Color.white);
graphics.fillRect(0, 0, imageWidth, imageHeight);
graphics.setColor(Color.red);
graphics.drawRect(X + 1 * columnWidth, columnHeight - h1, columnWidth, h1);
graphics.drawRect(X + 2 * columnWidth, columnHeight - h2, columnWidth, h2);
graphics.drawRect(X + 3 * columnWidth, columnHeight - h3, columnWidth, h3);
graphics.drawRect(X + 4 * columnWidth, columnHeight - h4, columnWidth, h4);
graphics.drawRect(X + 5 * columnWidth, columnHeight - h5, columnWidth, h5);
chartGraphics.createImage("D:\\myShare\\exe_file\\image\\keys.bmp");
}
catch(Exception e)
{
System.out.println("Instanction is :"+e.getMessage());
} } }
-----------------
第二步:创建另一个Java Bean从文本文件中读取数据(每一个长方形的高度),在实际应用中数据存储在Oracle数据库中 源程序如下:
package draw_test1;
import java.io.*;
public class GetData {
int heightArray[]=new int[5];
public int[] getHightArray() {
try {
RandomAccessFile randomAccessFile = new RandomAccessFile ("D:\\myShare\\exe_file\\image\\ColumnHeightArray.txt","r");
for(int i=0;i<5;i++)
{
heightArray[i] = Integer.parseInt(randomAccessFile.readLine());
} }
catch(Exception e) {
System.out.println(e);
}
return heightArray;
}}
------------------
第三步:创建JSP文件 源程序如下:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="draw_test1.ChartGraphics" %>
<%@ page import="draw_test1.GetData" %>
<jsp:useBean id="cg" class="ChartGraphics" scope="session"/>
<jsp:useBean id="gd" class="GetData" scope="session"/>
<%!
int height[]=new int[5];
%>
<%
height=gd.getHightArray();
cg.graphicsGeneration(height[0],height[1],height[2],height[3],height[4]);
%>
<html>
<body>
<img src="d:myShare\exe_file\image\keys.bmp></img>
</body>
</html>
-----------------------------
运行时错误如下显示:
Apache Tomcat/4.0.6 - HTTP Status 500 - Internal Server Errortype Exception reportmessage Internal Server Errordescription The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.exception javax.servlet.ServletException: ChartGraphics
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:471).......