HOWTO: Display a custom image on a jsp page
You need 4 files.
1. a jsp page (index.jsp)
2. A servlet (myPackage.ImageMaker)
3. A supplier class (myPackage.PluggableSupplier)
4. web.xml (your IDE should supply this)
From the jsp page you're calling the servlet with an <img> tag, and passing it some parameters.
The servlet obtains a BufferedImage from a pluggable supplier class, and returns with an image (png).
The image is displayed on the jsp page. Hope this helps!
================================================================================================
Here's the .jsp file (index.jsp)
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Image page</title>
</head>
<body>
<h1>Your image:</h1>
<img src="ImageMaker?Param1=Hello world&Param2=Hello again" />
</body>
</html>
================================================================================================
Here's the servlet (ImageMaker.java):
package myPackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.http.*;
publicclass ImageMakerextends HttpServlet{
protectedvoid processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
System.out.println("called");
//Prevent chacing of the image as it's probably intended to be 'dynamic'
response.addDateHeader("Expires", 795294400000);//<--long ago
response.addHeader("Cache-Control","no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
response.addHeader("Pragma","no-cache");
//set mime type and get output stream
response.setContentType("image/png");
OutputStream out = response.getOutputStream();
//catch incoming parameters, as many or few as you want
String parameter1 = request.getParameter("Param1");
String parameter2 = request.getParameter("Param2");
//fetch the buffered image
int width = 100;
int height = 50;
PluggableSupplier ps =new PluggableSupplier(width, height);
BufferedImage buf = ps.fetchBI(parameter1);
/***You can now draw on the original image if you want to add a watermark or label or whatever*********/
Graphics g = buf.getGraphics();
g.setColor(Color.BLACK);
g.drawString(parameter2, 5, 30);
try{
System.out.println("writing...");
ImageIO.write(buf,"png", out);
}catch(IOException ioe){
System.err.println("Error writing image file: " + ioe);
}
}
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
processRequest(request, response);
}
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
processRequest(request, response);
}
}
================================================================================================
This is the class that supplies your image (PluggableSupplier.java):
package myPackage;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
publicclass PluggableSupplier{
BufferedImage buf =null;
Graphics2D g2d =null;
int width = 50;
int height = 50;
public PluggableSupplier(int w,int h){
if ((w+h)>2){
this.width = w;
this.height = h;
}
buf =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2d = buf.createGraphics();
}
public BufferedImage fetchBI(String parameter1){
if (g2d !=null){
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.WHITE);
g2d.drawString(parameter1, 5, 15);
}
return buf;
}
}
================================================================================================
finally, your web.xml should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>ImageMaker</servlet-name>
<servlet-class>myPackage.ImageMaker</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageMaker</servlet-name>
<url-pattern>/ImageMaker</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
null

