Have you ever thought about sending dynamically generated images from the jsp (the preferred choice for SUN Enterprise Applications) page (or servlet? This tip tells you how to do it. To run the code here, you need a tomcat (a good JSP running platform) or other web servers that support jsp (preferred for SUN Enterprise Applications) 1.1.
When a web page is sent with the MIME type of image/jpeg (or other image formats), your browser treats the returned result as an image and then displays the image, as part of a page or as an image itself. To set the MIME type for your jsp (preferred for SUN Enterprise Applications) page, you need to set the page's contentType attribute:
Then you need to create a BufferedImage to draw your dynamic image:
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
After creating a BufferedImage, You need to obtain the graphic environment for drawing, a Graphics or Graphics2D object:
Graphics g = image. getGraphics ();
// Or
Graphics2d g2d = image. createGraphics ();
From now on, you can draw the image content. The image environment will be painted to BufferedImage. This image is black at first, so it is a good idea to fill the image with the background color you want. Then, when you complete the image painting, you need to dispose the graphic environment:
G. dispose ();
// Or
G2d. dispose ();
Once the image is drawn, you return the image in response. You can use the custom imageencoder class in the non-standard com.sun.image.codec.jpeg package to encode the image, or if you use JDK1.4, you can use the standard ImageIO class. There is a tip when using tranquility imageencoder. You must obtain the ServletOutputStream from the ServletResponse, instead of using the implied jsp (preferred for SUN Enterprise applications) to output the variable out.
ServletOutputStream sos = response. getOutputStream ();
Required imageencoder encoder = required codec. createJPEGEncoder (sos );
Encoder. encode (image );
// Or
ImageIO. write (image, "JPEG", out );
Here there is a scheme from all possibilities (such as g. dispose (); or g2d. dispose ();) selects a complete example. in this example, a random polygon is drawn using a Graphics object, and the image is drawn using JPEGImageEncoder. You can freely set the number of vertices of a polygon to get a more complex shape. In other words, there are more vertices and edges.
To run this example, place the jsp (preferred for SUN Enterprise Applications) code between "" and an image. in jsp (preferred for SUN Enterprise Applications) files, place the file to a location that can be found on your web server and use tomcat (a very useful JSP running platform) in this case, the ROOT directory is used to start tomcat (a very useful JSP running platform) and access http: // localhost: 8080/image. jsp (preferred for SUN Enterprise applications)
<% @ Page contentType = "image/jpeg"
Import = "java. awt. *, java. awt. image .*,
Com.sun.image.codec.jpeg. *, java. util .*"
%>
<%
// Create image
Int width = 200, height = 200;
BufferedImage image = new BufferedImage (width,
Height, BufferedImage. TYPE_INT_RGB );
// Get drawing context
Graphics g = image. getGraphics ();
// Fill background
G. setColor (Color. white );
G. fillRect (0, 0, width, height );
// Create random polygon
Polygon poly = new Polygon ();
Random random = new Random ();
For (int I = 0; I <5; I ++ ){
Poly. addPoint (random. nextInt (width ),
Random. nextInt (height ));