Dynamically create image from text using Java Servlet
This example shows how we can dynamically create an image from a text ('Hello World!') by writing a simple Java Servlet. In the next post, we'll see how this dynamic creation of image can be used to implement custom CAPTCHA validation.
Tools and Technologies used in this article :
JSP / Servlet
Eclipse 3.7
Tomcat
1. Create Dynamic Web Project
Select from the menu File --> New --> Dynamic Web Project (say 'ImageCreatorServlet').
2. Write a Java Servlet
Create a servlet class (say com.srccodes.examples.servlet.ImageCreator)
Modify the java class as per the following code.
3. Add servlet mapping
Add servlet mapping for the servlet ImageCreator in web.xml.
File : web.xml
4. Create a Jsp page
Create a jsp page (say test.jsp) and add a <img> html tag to display the dynamically generated image.
File : test.jsp
5. Overall Project Structure

6. Run Your Code
Right click on 'test.jsp' and select from context menu 'Run As' --> 'Run on Server'.
7. Browser Output
Eclipse will open a browser and an image of 'Hello World!' will be shown in the browser.

Download Source Code