This article tries to use a simple example to show you how to use JSP to call Javabean to dynamically generate a bar chart on the webpage. The following code is successfully tested on Windows. The Web application server uses Allaire jrun3.0. Step 1: Create a Java Bean to generate a JPG file The source program is as follows:
// Java Bean for generating images // By Cui guanyu // Date: 2001-08-24 Import java. Io .*; Import java. util .*; Import com.sun.image.codec.jpeg .*; Import java. AWT. image .*; Import java. AWT .*; Public class chartgraphics { Bufferedimage image; Public void createimage (string filelocation ){ Try { Fileoutputstream Fos = new fileoutputstream (filelocation ); Bufferedoutputstream Bos = new bufferedoutputstream (FOS ); Required imageencoder encoder = required codec. 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; // The image width. Int imageheight = 300; // The Image Height Int columnwidth = 30; // The width of the column Int columnheight = 200; // maximum column height 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: // temp // chart.jpg "); } } |
Explanation: The createimage (string filelocation) method is used to create a jpg image. The filelocation parameter is the file path. Graphicsgeneration (INT H1, int H2, int H3, int H4, int H5) is used to plot the image content. The parameter H1 ...... H5 is the height of each rectangle Step 2: create another Java Bean to read data from a text file (the height of each rectangle) and store the data in the Oracle database in practical applications. The source program is as follows:
// Java Bean for reading data in the text file // By Cui guanyu // Date: 2001-08-24 Import java. Io .*; Public class getdata { Int heightarray [] = new int [5]; Public int [] gethightarray (){ Try { Randomaccessfile = new randomaccessfile ("D: // temp // 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; } } |
Explanation: gethightarray () is used to read data from the text. It converts the string type in the text to the int type and returns the data of the array type. Step 3: Create a JSP file The source program is as follows:
<% @ Page import = "chartgraphics" %> <% @ Page import = "getdata" %> <JSP: usebean id = "CG" class = "chartgraphics"/> <JSP: usebean id = "GD" class = "getdata"/> <%! Int height [] = new int [5]; %> <% Height = Gd. gethightarray (); CG. graphicsgeneration (height [0], height [1], height [2], height [3], height [4]); %> <HTML> <Body> </img> </Body> </Html> |
JSP first calls Bean (getdata .. class) to read the data in the file, then calls Bean (chartgraphics. Class) to generate an image, and finally displays the image. The data in columnheightarray.txt can be changed at any time, so the height of the five rectangles in the generated image changes accordingly, so that the image can be dynamically generated. this design idea can also be used to create a website's voting system. |