Creating Images in a Java Servlet
Creating Images in a Java Servlet |
by: Kevin Long
Dynamic images are commonly used in web applications. You will find dynamic images such as charts, captcha, web site thumbnails, image thumbnails, watermarks, etc. This tutorial will give you a brief walk through on creating a simple dynamic image in a Java Servlet.
Setting up the Servlet
As with most servlets, this servlet will override the doGet() method in HttpServlet. The doGet() method will be called when an HTTP GET request is made to the servlet. We can expect an HTTP GET request when a servlet is called within an <img> tag.
package com.codebeach.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class ImageServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
{
}
}
Creating the Image
We will create a BufferedImage for drawing our demo image. When creating the BufferedImage, you can specify the size (width x height) and the type of image.
BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
Drawing Our Image
Once we have an image, we can get the Graphics object from the bufferedImage and draw our image. For this example, we will draw a simple circle on the image. After the oval is drawn, the Graphics object is disposed.
//Draw an oval
Graphics g = bufferedImage.getGraphics();
g.setColor(Color.blue);
g.fillOval(0, 0, 199,199);
g.dispose();
Setting the Image Mime Type
Normally, when a static image is sent from the web server to the web browser, the web server knows the mime type of the image based on the file extension. If the file was a jpg, the mime type would be image/jpg. If the file was a gif, the mime type would be image/gif. Since the web server won't have a file extension for the mime type, we will need to set the mime type for web browser. For our example, we are going to create a jpg image.
res.setContentType("image/jpg");
Creating a JPG
The final step is to write the image to the response output stream as a jpg. To do this, we will use the ImageIO class that was introduced with Java 1.4. The ImageIO class allows you to write an Image object to JPG, PNG, BMP, and WBMP. In Java 1.6, you will be able to write an Image as GIF.
ImageIO.write(bufferedImage, "jpg", res.getOutputStream());
The ImageIO class will write the bufferedImage as a jpg to the output stream from the HttpServletResponse object.
The Results
To access the image, you can place the servlet call in an <img> tag or directly from the URL. The following is a screenshot from of the generated image in a web browser.
Wrapping It Up
Below is the complete example of creating an image from a servlet:
package com.codebeach.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class ImageServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
{
//Set the mime type of the image
res.setContentType("image/jpg");
try
{
//Create an image 200 x 200
BufferedImage bufferedImage = new BufferedImage(200, 200,
BufferedImage.TYPE_INT_RGB);
//Draw an oval
Graphics g = bufferedImage.getGraphics();
g.setColor(Color.blue);
g.fillOval(0, 0, 199,199);
//Free graphic resources
g.dispose();
//Write the image as a jpg
ImageIO.write(bufferedImage, "jpg", res.getOutputStream());
}
catch (IOException ioe)
{
}
}
}