Sun Java Solaris Communities My SDN Account Join SDN
 
Technical Articles and Tips

Tech Tips - Update

 

Tech Tips Archive


This is an update to the the Java Developer Connection (JDC) Tech Tip for August 21, 2001 on Delivering Dynamic Images from JavaServer Pages (JSP) Technology. You can view that issue of the Tech Tips on the Web at http://java.sun.com/jdc/JDCTechTips/2001/tt0821.html

That tip mistakenly generated binary content from a JSP page. The Servlet specification requires that servlets not call both ServletResponse.getOutputStream() and ServletResponse.getWriter(). Because JSP pages already call getWriter(), they must not also call getOutputStream(). Some versions of Tomcat delay the calling of getWriter(), so that the tip might work for those versions. However the operations demonstrated in the tip should be considered illegal. JSP pages are only meant to deliver textual output.

In order for this tip to work properly, the dynamic image generation must be moved into a servlet. Place the following class definition in an appropriate servlet directory for your web server (such as webapps\ROOT\WEB-INF\classes for Tomcat). Then you should be able to call it with a URL of http://localhost:8080/servlet/MakeImage. The core code is identical to that used previously in the tip, except that it's now part of a servlet.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import java.util.*;

public class MakeImage extends HttpServlet {

   public void doGet(
       HttpServletRequest request,
       HttpServletResponse response)
         throws ServletException, IOException {

     response.setContentType("image/jpeg");

     // Create image
     int width=200, height=200;
     BufferedImage image = new BufferedImage(
       width, height, BufferedImage.TYPE_INT_RGB);

     // Get drawing context
     Graphics g = image.getGraphics();

     // Fill background
     g.setColor(Color.white);
     g.fillRect(0, 0, width, height);

     // Create random polygon
     Polygon poly = new Polygon();
     Random random = new Random();
     for (int i=0; i < 20; i++) {
       poly.addpoint(random.nextint(width),
         random.nextint(height));
     }

     // fill polygon
     g.setcolor(color.cyan);
     g.fillpolygon(poly);

     // dispose context
     g.dispose();

     // send back image
     servletoutputstream sos =
       response.getoutputstream();
     jpegimageencoder encoder =
       jpegcodec.createjpegencoder(sos);
     encoder.encode(image);

   }
}

In addition, if you are a Unix user running a web server on a headless box, you might need Xvfb, which is a virtual frame buffer X server, to emulate a display environment. See http://java.sun.com/products/java-media/2D/forDevelopers/java2dfaq.html#xvfb for more information about server-side support for working with images.

Pixel

- NOTE

Sun respects your online time and privacy. The Java Developer Connection mailing lists are used for internal Sun Microsystems purposes only. You have received this email because you elected to subscribe. To unsubscribe, go to the Subscriptions page, uncheck the appropriate checkbox, and click the Update button.

As of May 22, 2001, Sun Microsystems updated its Privacy Policy to give you a better understanding of Sun's Privacy Policy and Practice. If you have any questions, contact privacy@sun.com.

- SUBSCRIBE

To subscribe to a JDC newsletter mailing list, go to the Subscriptions page, choose the newsletters you want to subscribe to, and click Update.

- FEEDBACK

Comments? Send your feedback on the JDC Tech Tips to:

jdc-webmaster@sun.com

- ARCHIVES

You'll find the JDC Tech Tips archives at: http://java.sun.com/jdc/TechTips/index.html

- COPYRIGHT

Copyright 2001 Sun Microsystems, Inc. All rights reserved. 901 San Antonio Road, Palo Alto, California 94303 USA.

This document is protected by copyright. For more information, see:

http://java.sun.com/jdc/copyright.html

- LINKS TO NON-SUN SITES

The JDC Tech Tips may provide, or third parties may provide, links to other Internet sites or resources. Because Sun has no control over such sites and resources, You acknowledge and agree that Sun is not responsible for the availability of such external sites or resources, and does not endorse and is not responsible or liable for any Content, advertising, products, or other materials on or available from such sites or resources. Sun will not be responsible or liable, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with use of or reliance on any such Content, goods or services available on or through any such site or resource.

This issue of the JDC Tech Tips is written by John Zukowski.

JDC Tech Tips August 23, 2001

Sun, Sun Microsystems, Java, Java Developer Connection, JavaServer Pages, and JSP are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.