You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Christopher Cain <cc...@mhsoftware.com> on 2001/08/24 04:35:39 UTC
Fwd: Correction to JDC Tech Tip for August 21, 2001
LOL! That poor cat. Although I'm surprised that the JDC crew didn't forward it
to Edwardo & Company and/or us for review ... you know, before sending it out
to a couple hundred thousand developers, give or take.
I assume that:
"Some versions of Tomcat delay the calling of getWriter(), so that the tip
might work for those versions."
... is marketing-speak for, "Well, we tested it on Tomcat 3.1, and it worked
for us." =)
So, what I want to know is this: When all of the e-mails start flooding the
user list ... "I tried out this one Tech Tip, but I got an error. Tomcat
bug!!!" ... how are you Sun peeps (Craig, Costin, Pier, Amy, et. al.) going to
decide who gets to take the elevator up to the JDC floor and start busting
heads? You gonna draw straws or something?
;-)
----- Forwarded message from JDC Tech Tips <body_-
5526453924590946636@hermes.java.sun.com> -----
Date: Thu, 23 Aug 2001 19:03:23 PDT
From: JDC Tech Tips <bo...@hermes.java.sun.com>
Reply-To: JDC Tech Tips <bo...@hermes.java.sun.com>
Subject: Correction to JDC Tech Tip for August 21, 2001
To: ccain@mhsoftware.com
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?-5526453924590946636
----- End forwarded message -----
Chistopher Cain
Re: Correction to JDC Tech Tip for August 21, 2001
Posted by Christopher Cain <cc...@mhsoftware.com>.
I _KNEW_ you weren't going to let us down on this one ... any rather
embarrasing blunder with JSP just really cries out for a Jon reply.
I was originally going to ask the (smart-ass) question as to why they
didn't go with Velocity for that particular tip, but we made Pier kinda
made last time. I figured I would just wait for your inevitable comments
;-)
- Christopher ... who promises not to say another word about it :-)
Jon Stevens wrote:
>
> > JSP pages are only meant to deliver textual
> > output.
>
> Velocity templates can be used to deliver binary output.
>
> <http://jakarta.apache.org/velocity/>
>
> Needless to say, I was surprised to see yet another example (coming from Sun
> no less) of someone embedding Java code in a JSP page.
>
> Paging all K-Mart shoppers: Please pull your head out of your ass.
>
> :-)
>
> -jon
Re: Correction to JDC Tech Tip for August 21, 2001
Posted by Jon Stevens <jo...@latchkey.com>.
> JSP pages are only meant to deliver textual
> output.
Velocity templates can be used to deliver binary output.
<http://jakarta.apache.org/velocity/>
Needless to say, I was surprised to see yet another example (coming from Sun
no less) of someone embedding Java code in a JSP page.
Paging all K-Mart shoppers: Please pull your head out of your ass.
:-)
-jon