Home arrow Java Servlets arrow Servlet to view Browser Information while visiting a site as a JPEG image Friday, 05 December 2008
Home
- - - - - - -
Contact me!
My Blog!
- - - - - - -
- - - - - - -
8086 Assembly Programs
Graphics
Java Servlets
Prolog
VHDL
- - - - - - -
Ubuntu Linux
- - - - - - -
Links
Statistics
Visitors: 43316

   
Servlet to view Browser Information while visiting a site as a JPEG image
Written by Rohit   
Monday, 10 April 2006

Please be patient while the image loads, this code is hosted on a free host!
  1. public class ImgServlet extends HttpServlet
  2. {
  3.     public void init(ServletConfig config) throws ServletException
  4.     {
  5.         super.init(config);
  6.     }
  7.     public void destroy()
  8.     {
  9.     }
  10.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  11.     throws ServletException, IOException
  12.     {
  13.         try
  14.         {
  15.             String text= "" ;
  16.             Vector v=new Vector();
  17.             Font font = Font.decode("Arial-BOLD-18");
  18.             Color background = Color.white;
  19.             Color color = Color.black;
  20.             BufferedImage buffer = new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
  21.             Graphics2D g2 = buffer.createGraphics();
  22.             FontRenderContext fc = g2.getFontRenderContext();
  23.             Rectangle2D bounds = font.getStringBounds(text,fc);
  24.             int width = (int) bounds.getWidth();
  25.             int height = (int) bounds.getHeight();
  26.             for (Enumeration e=request.getHeaderNames(); e.hasMoreElements() ;)
  27.             if (e==null)
  28.             break;
  29.             else
  30.             {
  31.                 String h=(String)e.nextElement();
  32.                 text=h+" : "+request.getHeader(h)+"\n";
  33.                 v.add(text);
  34.                 bounds = font.getStringBounds(text,fc);
  35.                 if (bounds.getWidth()>width)
  36.                 width=(int)bounds.getWidth();
  37.                 height+=bounds.getHeight();
  38.             }
  39.             text="Remote Address : "+request.getRemoteAddr()+"\n";
  40.             v.add(text);
  41.             if (bounds.getWidth()>width)
  42.             width=(int)bounds.getWidth();
  43.             height+=bounds.getHeight();
  44.             text="Remote Host : "+request.getRemoteHost()+"\n";
  45.             v.add(text);
  46.             if
  47.             (bounds.getWidth()>width)
  48.             width=(int)bounds.getWidth();
  49.             height+=bounds.getHeight();
  50.             text="Server Name : "+request.getServerName()+"\n";
  51.             v.add(text);
  52.             if (bounds.getWidth()>width)
  53.             width=(int)bounds.getWidth();
  54.             height+=bounds.getHeight();
  55.             text="Protocol : "+request.getProtocol()+"\n";
  56.             v.add(text);
  57.             if (bounds.getWidth()>width)
  58.             width=(int)bounds.getWidth();
  59.             height+=bounds.getHeight();
  60.             buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  61.             g2 = buffer.createGraphics();
  62.             g2.setFont(font);
  63.             g2.setColor(background);
  64.             g2.fillRect(0,0,width,height);
  65.             g2.setColor(color);
  66.             for (int j=0; j<v.size();j++)
  67.             {
  68.                 g2.drawString((String)v.elementAt(j),0,(int)-bounds.getY()*j+20);
  69.             }
  70.             response.setContentType("image/jpeg");
  71.             OutputStream os = response.getOutputStream();
  72.             ImageIO.write(buffer, "jpeg", os);
  73.             os.close();
  74.         }
  75.         catch (java.lang.Exception ex)
  76.         {
  77.             response.setContentType("text/html");
  78.             PrintWriter out = response.getWriter();
  79.             out.println( "** Error ** " );
  80.             ex.printStackTrace(out);
  81.         }
  82.     }
  83.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  84.     throws ServletException, IOException
  85.     {
  86.         processRequest(request, response);
  87.     }
  88.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  89.     throws ServletException, IOException
  90.     {
  91.         processRequest(request, response);
  92.     }
  93.     public String getServletInfo()
  94.     {
  95.         return "The servlet displays HTTP headers!";
  96.     }
  97. }
 
Next >

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.