Java Hosting » Servlet Hosting » JSP tutorial » Image servlet

Image servlet

Beside html pages, servlet can also return binary data (images, compressed data, etc).

Any format of image can be used if an encoder in Java is available and if the Web Clients understands. Read about what our experts can provide you on java web hosting website.

Understanding image encoding helps you understand how servlets handle images. A servlet like ViewFile can return a preexisting image by sending its encoded representation unmodified to the client the browser decodes the image for viewing.

But a servlet that generates or modifies an image must construct an internal representation of that image, manipulate it, and then encode it, before sending it to the client.

This code is an example of creating an image with string, which will discuss later

import java.io.*; import java.awt.*; import javax.servlet.*; import javax.servlet.http.*; import Acme.JPM.Encoders.GifEncoder; public class HelloWorldGraphics extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ServletOutputStream out = res.getOutputStream(); Frame frame = null; Graphics g = null; try { // Create an frame frame frame = new Frame(); frame.addNotify(); // We get a graphic region using a frame Image image = frame.createImage(600, 80); g = image.getGraphics(); // Draw "Hello World!" to the off-screen graphics context g.setFont(new Font("Verdana", Font.BOLD, 58)); g.drawString("A Test Image!", 15, 55); // Encode a gif image and send it to the client res.setContentType("image/gif"); GifEncoder encoder = new GifEncoder(image, out); encoder.encode(); } finally { // Clean up resources if (g != null) g.dispose(); if (frame != null) frame.removeNotify(); } } }

To optain an off-screen image requires to jump through several hoops. Java language uses java.awt.Image class.

You can't instantiated directly through a constructor an image object, it must be done through the method like the createImage() method of Component or the getImage() method of Toolkit.

We will use createImage() because we are creating a new image. Before you can create an image, you must know that it's native peer must exist. To create an image requires to create a frame, thus we will use addNotify(), and then use the frame to create our Image.

When the image is created we will draw onto it using its graphics context, which can be retrieved with a call to the getGraphics() method of Image. In this code we just draw a simple string. When we finish drawing into the graphic context, setContentType() is called to set the MIME type to "image/gif" since we will use the GIF encoding.

For an image to be encoded we will create a GifEncoder object, passing it the image object and the ServletOutputStream for the servlet. When encode() is called on the Gif Encoder object, the image is encoded and sent to the client. After the image is sent, the graphical resources are released.

These would be reclaimed automatically during garbage collection, but the system resources are released if we release them immediately. We placed the code to release the resources in a finally block to guarantee its execution, even when the servlet throws an exception.

Contact sales!