Return-Path: Received: from pacific-carrier-annex.mit.edu by po10.mit.edu (8.9.2/4.7) id XAA14934; Thu, 23 Aug 2001 23:04:33 -0400 (EDT) Received: from hermes.java.sun.com (hermes.java.sun.com [204.160.241.85]) by pacific-carrier-annex.mit.edu (8.9.2/8.9.2) with SMTP id XAA09690 for ; Thu, 23 Aug 2001 23:04:32 -0400 (EDT) Message-Id: <200108240304.XAA09690@pacific-carrier-annex.mit.edu> Date: Thu, 23 Aug 2001 20:04:32 PDT From: "JDC Tech Tips" To: alexp@mit.edu Subject: Correction to JDC Tech Tip for August 21, 2001 Precedence: junk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Beyond Email 2.2 J D C T E C H T I P S TIPS, TECHNIQUES, AND SAMPLE CODE This is an update to the the Java Developer Connection(sm) (JDC) Tech Tip for August 21, 2001 on Delivering Dynamic Images from JavaServer Pages(tm) (JSP(tm)) 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. . . . . . . . . . . . . . . . . . . . . . . . - NOTE Sun respects your online time and privacy. The Java Developer Connection mailing lists are used for internal Sun Microsystems(tm) purposes only. You have received this email because you elected to subscribe. To unsubscribe, go to the Subscriptions page http://developer.java.sun.com/subscription/ uncheck the appropriate checkbox, and click the Update button. - SUBSCRIBE To subscribe to a JDC newsletter mailing list, go to the Subscriptions page http://developer.java.sun.com/subscription/ 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. 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. To use our one-click unsubscribe facility, select the following URL: http://hermes.java.sun.com/unsubscribe?-5852066593234543465