tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Tomcat Wiki] Trivial Update of "FAQ/KnownIssues" by KonstantinPreisser
Date Mon, 22 Aug 2011 14:22:22 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tomcat Wiki" for change notification.

The "FAQ/KnownIssues" page has been changed by KonstantinPreisser:
http://wiki.apache.org/tomcat/FAQ/KnownIssues?action=diff&rev1=4&rev2=5

Comment:
Better usage of try-with-resources; correct typos/wording

  <<BR>>
  <<Anchor(ImageIOIssues)>>'''I'm using the Java ImageIO to dynamically serve
images and get strange Exceptions from time to time. Is this a bug in Tomcat?'''
  
- Imagine you have a servlet which dynamically generates images and serves them via the Java
ImageIO. To write the image to the OutputStream, perhaps you are doing something like this:
+ Imagine you have a servlet which dynamically generates images and serves them via javax.imageio.ImageIO.
To write the image to the OutputStream, perhaps you are doing something like this:
  
  {{{
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
          BufferedImage img = createMyImage(); // makes a BufferedImage
          
          response.setContentType("image/png");
-         try {
-             try (OutputStream out = response.getOutputStream()) { // try-with-resources
+         try (OutputStream out = response.getOutputStream()) { // try-with-resources
-                 ImageIO.write(img, "PNG", out);
+             ImageIO.write(img, "PNG", out);
-             }
          } catch (IOException ex) {
-             // Client abandoned connection
+             // Client aborted connection
          }
      }
  }}}
  
- Now, although there shouldn't be any Exception logged (because the IOException which occurs
when the client abandoned the connection is ignored), you see strange Exceptions in Tomcat's
log which may belong to other Servlets/JSP (at least with Sun/Oracle JVM on Windows), saying
that the response has already commited, althought you didn't write anything to it at that
time. For example:
+ Now, although there shouldn't be any Exception logged (because the IOException which occurs
when the client aborted the connection is ignored), you see strange Exceptions in Tomcat's
log which may belong to other Servlets/JSP (at least with Sun/Oracle JVM on Windows), saying
that the response has already commited, althought you didn't write anything to it at that
time. For example:
  {{{
  13.07.2011 00:13:51 org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [myApp.MyServlet] in context with path [] threw exception
@@ -128, +126 @@

  Now you just have to use this Decorater class instead of using Tomcat's OutputStream directly:
  {{{
          response.setContentType("image/png");
-         try {
-             try (OutputStream out = new MyImageIOOutputStream(response.getOutputStream()))
{
+         try (OutputStream out = new MyImageIOOutputStream(response.getOutputStream())) {
-                 ImageIO.write(img, "PNG", out);
+             ImageIO.write(img, "PNG", out);
-             }
          } catch (IOException ex) {
-             // Client abandoned connection
+             // Client aborted connection
          }
  }}}
  and the errors should be gone away.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message