public class ImgServlet extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void destroy()
{
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
String text= "" ;
Vector v=new Vector();
Font font = Font.decode("Arial-BOLD-18");
Color background = Color.white;
Color color = Color.black;
BufferedImage buffer = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = buffer.createGraphics();
FontRenderContext fc = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(text,fc);
int width = (int) bounds.getWidth();
int height = (int) bounds.getHeight();
for (Enumeration e=request.getHeaderNames(); e.hasMoreElements() ;)
if (e==null)
break;
else
{
String h=(String)e.nextElement();
text=h+" : "+request.getHeader(h)+"\n";
v.add(text);
bounds = font.getStringBounds(text,fc);
if (bounds.getWidth()>width)
width=(int)bounds.getWidth();
height+=bounds.getHeight();
}
text="Remote Address : "+request.getRemoteAddr()+"\n";
v.add(text);
if (bounds.getWidth()>width)
width=(int)bounds.getWidth();
height+=bounds.getHeight();
text="Remote Host : "+request.getRemoteHost()+"\n";
v.add(text);
if
(bounds.getWidth()>width)
width=(int)bounds.getWidth();
height+=bounds.getHeight();
text="Server Name : "+request.getServerName()+"\n";
v.add(text);
if (bounds.getWidth()>width)
width=(int)bounds.getWidth();
height+=bounds.getHeight();
text="Protocol : "+request.getProtocol()+"\n";
v.add(text);
if (bounds.getWidth()>width)
width=(int)bounds.getWidth();
height+=bounds.getHeight();
buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2 = buffer.createGraphics();
g2.setFont(font);
g2.setColor(background);
g2.fillRect(0,0,width,height);
g2.setColor(color);
for (int j=0; j<v.size();j++)
{
g2.drawString((String)v.elementAt(j),0,(int)-bounds.getY()*j+20);
}
response.setContentType("image/jpeg");
OutputStream os = response.getOutputStream();
ImageIO.write(buffer, "jpeg", os);
os.close();
}
catch (java.lang.Exception ex)
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println( "** Error ** " );
ex.printStackTrace(out);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
public String getServletInfo()
{
return "The servlet displays HTTP headers!";
}
}