A sample servlet that dynamically creates images

This is a sample servlet that dynamically generates JPEG images. Currently all image properties are hard-coded, but it should be trivial to provide a jsp form to collect them (e.g., color, font, dimensions, messages). The generated image contains dynamic data such as the appserver name, and OS name.
package javahowto;

import com.sun.image.codec.jpeg.JPEGCodec;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class ImageServlet extends HttpServlet {
private static final int WIDTH = 450;
private static final int HEIGHT = 200;
private static final Color BACKGROUND_COLOR = new Color(100,149,237);
private static final Color COLOR = new Color(0,0,139);
private static final Font FONT = new Font("Times New Roman", Font.BOLD, 46);
private static final Font FOOT_FONT = new Font("Courier", Font.ITALIC, 14);
private static final Color FOOT_COLOR = Color.BLACK;

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("image/jpg");
ServletOutputStream out = response.getOutputStream();
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_INDEXED);
Graphics graphics = image.getGraphics();
graphics.setColor(BACKGROUND_COLOR);
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
graphics.setColor(COLOR);
graphics.setFont(FONT);
graphics.drawString("Hello World!", 10, HEIGHT/2);
graphics.setFont(FOOT_FONT);
graphics.setColor(FOOT_COLOR);
graphics.drawString("Created by " + getServletContext().getServerInfo(), 10, HEIGHT - 30);
graphics.drawString("for http://javahowto.blogspot.com/ on " + System.getProperty("os.name"), 10, HEIGHT - 10);
JPEGCodec.createJPEGEncoder(out).encode(image);
}
}
The web.xml file is as simple as declaring and mapping a servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>image</servlet-name>
<servlet-class>javahowto.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>image</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Build the project and deploy the image.war to any web server or appserver. Enter the URL http://localhost:8080/image/ to view the generated image. Here we are using the .war file base name as the default context-root and a / servlet path. I tested it on Glassfish v2 update 1, Tomcat 6, and JBoss-5.0.0.Beta4 successfully, except some redeployment errors on JBoss, which was resolved by deleting the .war and restarting server.

If the appserver is running on a headless server, you may need to set the system property java.awt.headless to true. Also note that a Sun-specific encoder class (com.sun.image.codec.jpeg.JPEGCodec) is used. It is in rt.jar of Sun's JDK, but may not be available in other vendors' JDK like IBM's JDK or BEA's (now Oracle's) JRockit. If a non-Sun Java is used, you will need to use the appropriate encoder class. Unfortunately, there seems to be no portable JPEG encoder class as of JDK 6.



Java enum examples

Simple enum. The ; after the last element is optional, when this is the end of enum definition.
public enum Color {
WHITE, BLACK, RED, YELLOW, BLUE; //; is optional
}
Enum embedded inside a class. Outside the enclosing class, elements are referenced as Outter.Color.RED, Outter.Color.BLUE, etc.
public class Outter {
public enum Color {
WHITE, BLACK, RED, YELLOW, BLUE
}
}
Enum that overrides toString method. A semicolon after the last element is required to be able to compile it. More details on overriding enum toString method can be found here.
public enum Color {
WHITE, BLACK, RED, YELLOW, BLUE; //; is required here.

@Override public String toString() {
//only capitalize the first letter
String s = super.toString();
return s.substring(0, 1) + s.substring(1).toLowerCase();
}
}
Enum with additional fields and custom constructor. Enum constructors must be either private or package default, and protected or public access modifier is not allowed. When custom constructor is declared, all elements declaration must match that constructor.
public enum Color {
WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25);

private int code;

private Color(int c) {
code = c;
}

public int getCode() {
return code;
}
Enum that implements interfaces. Enum can implement any interfaces. All enum types implicitly implements java.io.Serializable, and java.lang.Comparable.
public enum Color implements Runnable {
WHITE, BLACK, RED, YELLOW, BLUE;

public void run() {
System.out.println("name()=" + name() +
", toString()=" + toString());
}
}
A sample test program to invoke this run() method:
for(Color c : Color.values()) {
c.run();
}
Or,
for(Runnable r : Color.values()) {
r.run();
}

Is the main method inherited?

Is the public static void main method on the superclass inherited by subclasses? Yes, it can be accessed by all subclasses directly. So it is possible to have a main method just on the superclass, and be able to run each subclass that doesn't itself declare the main method. For example:
public class Main {
public static void main(String[] args) {
System.out.println("In Main.main(String[])");
}
}

public class Main2 extends Main {
public void m2() {
main(null); //access the main method in superclass
}
}

public class Main3 extends Main2 {}
Running Main, Main2, Main3 have the same effect:
C:\tmp> java Main
In Main.main(String[])

C:\tmp> java Main2
In Main.main(String[])

C:\tmp> java Main3
In Main.main(String[])
When running Main2 and Main3 inside NetBeans 6.1 beta, I got the error Class "Main2" does not have a main method. Note this error is not from java runtime. It seems NetBeans is doing some validation and being too restrictive.

Followers

Pageviews Last 7 Days