ImageServlet

Generates dynamic shape (PNG) images, intended for (JSP) page decoration, like rounded corners, arrows etc.

WAR Packaging

The library JAR file (imageservlet-1.0.jar) must be added to the /WEB-INF/lib directory of your webapp WAR file.

Servlet Configuration

The servlet must be defined in the web.xml file of your webapp WAR. The snippet below shows the servlet definition and url mapping to /img/*.

<servlet>
    <servlet-name>ImageServlet</servlet-name>
    <servlet-class>com.ribomation.imageservlet.ImageServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>ImageServlet</servlet-name>
    <url-pattern>/img/*</url-pattern>
</servlet-mapping>
                

The servlet also accepts the init parameters listed below.

NameMandatoryDescription
configFilenoName of properties file (e.g. /WEB-INF/imageservlet.properties) with default values.
configDatanoNewline separated list key=value pairs, with default values.
shapeClassesnoComma separated list of shape classes to load into the shape factory.
colorDefsnoComma separated list of colorName=hexCode pairs to load into the color factory.

Example configuration.

<servlet>
    <servlet-name>ImageServlet</servlet-name>
    <servlet-class>com.ribomation.imageservlet.ImageServlet</servlet-class>
    <init-param>
        <param-name>shapeClasses</param-name>
        <param-value>com.whatever.shapes.FooShape,com.whatever.shapes.BarShape</param-value>
    </init-param>
</servlet>
                

Servlet Invocation

The image servlet is invoked from an ordinary IMG HTML tag, were the SRC attribute referrs to the servlet (url mapping) plus request parameters. The snippet below shows an example.

<img width="50" height="50" src="/YOUR-WEBAPP/img?shape=rhomb&color=red&bgcolor=silver&width=50&height=50">
                

The request parameters accepted are listed in the table below.

NameMandatoryDefaultDescription
shapenorhombThe shape to paint
widthno50Width in pixels of the image
heightno50Height in pixels of the image
colornoredColor of the image
bgcolornosilverBackground color of the image

The built-in shapes are listed in the table below. However, it's easy to define new shape types, or redefine an existing ditto. Just sub-class AbstractShape and then add a shapeClasses init parameter to the servlet.

Shape NameDescription
rectRectangle
ovalCircle or ellipse
rhombRhomb
triangle_downTriangle pointing down
triangle_leftTriangle pointing left
triangle_rightTriangle pointing right
triangle_upTriangle pointing up
corner_round_top_leftSquared image with a quarter circle area in quadrant top-left
corner_round_top_rightSquared image with a quarter circle area in quadrant top-right
corner_round_bottom_leftSquared image with a quarter circle area in quadrant bottom-left
corner_round_bottom_rightSquared image with a quarter circle area in quadrant bottom-right
corner_slope_top_leftSquared image with a quarter sloped/diagonal area in quadrant top-left
corner_slope_top_rightSquared image with a quarter sloped/diagonal area in quadrant top-right
corner_slope_bottom_leftSquared image with a quarter sloped/diagonal area in quadrant bottom-left
corner_slope_bottom_rightSquared image with a quarter sloped/diagonal area in quadrant bottom-right

A color name, might be one of the predefined colors listed in the table below or a web color HEX code, like 'FFEEDD'.

Color NameHEX Value
black000000
whiteFFFFFF
redFF0000
green00FF00
blue0000FF
yellowFFFF00
orangeFFA500
silverC0C0C0
gray808080
maroon800000
purple800080
fuchsiaFF00FF
lime008000
olive808000
navy000080
teal008080
aqua00FFFF

ImageServlet Demo

The distribution comes with a small demo WAR, were you can try out different settings and view the generated image. The demo is also available online.

Click here to view the ImageServlet demo.