javawebparts.servlet
Class TextRendererServlet

java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--javawebparts.servlet.TextRendererServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class TextRendererServlet
extends javax.servlet.http.HttpServlet

This servlet serves a dynamically-rendered image of a string of text with the specified font characteristics.

Example configuration in web.xml:

<servlet>
  <servlet-name>TextRendererServlet</servlet-name>
  <servlet-class>javawebparts.servlet. TextRendererServlet</servlet-class>
  <init-param>
    <param-name>fontName</param-name>
    <param-value>arial</param-value>
  </init-param>
  <init-param>
    <param-name>fontPoint</param-name>
    <param-value>24</param-value>
  </init-param>
  <init-param>
    <param-name>color</param-name>
    <param-value>255,0,0 </param-value>
  </init-param>
  <init-param>
    <param-name>backColor</param-name>
    <param-value>255,255,0 </param-value>
  </init-param>
  <init-param>
    <param-name>stylePlain</param-name>
    <param-value>false</param-value>
  </init-param>
  <init-param>
    <param-name>styleBold</param-name>
    <param-value>true</param-value>
  </init-param>
  <init-param>
    <param-name>styleItalic</param-name>
    <param-value>true</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>TextRendererServlet</servlet-name>
  <url-pattern>/textRenderer</url-pattern>
</servlet-mapping>

Init parameters explained:


All of the above parameters can be passed in as request parameters to the servlet as well, and the request parameters will override the servlet init paramter values. Also note that all of the above init parameters are optional. However, the font, fontPoint, color and backColor parameters ARE IN FACT REQUIRED FOR THE SERVLET TO FUNCTION! They MUST be supplied either as init values or as request parameters. You can mix and match as appropriate, i.e., if you want to only render in Arial, then set the fontName init parameter and do not pass a fontName request parameter in. IF a particular parameter is found as neither an init parameter or a request parameter, the above specified default values are used. If the text parameter is not found in request, a blank image will be returned, but no error will occur.

The most common usage of this servlet is usually to use it as the src of an <img> tag with some or all of the required parameters attached as a query string. For example:

<img src="/TextRenderer?fontPoint=32&text=testing123">

The method that actually renders the text, appropriately named renderText(), is public and stand-alone and available to any outside code, so if you want to use the functionality of the servlet from your own code without actually using it as a servlet, simply instantiate it as any other class and call this method. It accepts a Map which contains the following elements:


They should all be familiar. colR, colG and colB are the RGB components of the foreground color, and backR, backG and backB are the RGB components of the background color. It returns an OutputStream (ByteArrayOutputStream specifically) whichis the rendered image. You can do whatever you want to with it at that point.

Author:
Frank W. Zammetti.
See Also:
Serialized Form

Field Summary
private  java.lang.Integer initBackB
          backB.
private  java.lang.Integer initBackG
          backG.
private  java.lang.Integer initBackR
          backR.
private  java.lang.Integer initColB
          colB.
private  java.lang.Integer initColG
          colG.
private  java.lang.Integer initColR
          colR.
private  java.lang.String initFontName
          font.
private  java.lang.Integer initFontPoint
          fontPoint.
private  java.lang.String initStyleBold
          styleBold.
private  java.lang.String initStyleItalic
          styleItalic.
private  java.lang.String initStylePlain
          stylePlain.
private static org.apache.commons.logging.Log log
          Log instance.
 
Fields inherited from class javax.servlet.http.HttpServlet
 
Fields inherited from class javax.servlet.GenericServlet
 
Constructor Summary
TextRendererServlet()
           
 
Method Summary
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          doGet.
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          doPost.
 void init(javax.servlet.ServletConfig config)
          init.
static java.io.OutputStream renderText(java.util.Map m)
          This method is the method which actually renders a given text string using the given font characteristics and returns the image as an OutputStream (ByteArrayOutputStream specifically).
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static org.apache.commons.logging.Log log
Log instance.


initFontName

private java.lang.String initFontName
font.


initFontPoint

private java.lang.Integer initFontPoint
fontPoint.


initStylePlain

private java.lang.String initStylePlain
stylePlain.


initStyleBold

private java.lang.String initStyleBold
styleBold.


initStyleItalic

private java.lang.String initStyleItalic
styleItalic.


initColR

private java.lang.Integer initColR
colR.


initColG

private java.lang.Integer initColG
colG.


initColB

private java.lang.Integer initColB
colB.


initBackR

private java.lang.Integer initBackR
backR.


initBackG

private java.lang.Integer initBackG
backG.


initBackB

private java.lang.Integer initBackB
backB.

Constructor Detail

TextRendererServlet

public TextRendererServlet()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
init.

Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class javax.servlet.GenericServlet
Parameters:
config - ServletConfig.
Throws:
javax.servlet.ServletException - ServletException.

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws javax.servlet.ServletException,
                  java.io.IOException
doGet. Calls doPost() to do real work.

Overrides:
doGet in class javax.servlet.http.HttpServlet
Parameters:
request - HTTPServletRequest.
response - HTTPServletResponse.
Throws:
javax.servlet.ServletException - ServletException.
java.io.IOException - IOExcpetion.

doPost

public void doPost(javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException,
                   java.io.IOException
doPost.

Overrides:
doPost in class javax.servlet.http.HttpServlet
Parameters:
request - HTTPServletRequest
response - HTTPServletResponse
Throws:
javax.servlet.ServletException - ServletException
java.io.IOException - IOExcpetion

renderText

public static java.io.OutputStream renderText(java.util.Map m)
                                       throws java.io.IOException
This method is the method which actually renders a given text string using the given font characteristics and returns the image as an OutputStream (ByteArrayOutputStream specifically).

Parameters:
m - A Map containing all the required elements. See the javadocs for this class for details.
Returns:
An OutputStream (ByteArrayOutputStream specifically) that is the final rendered image.
Throws:
java.io.IOException - IOException


Copyright © 2005 Frank W. Zammetti