You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Chris Gross <cg...@chessys.com> on 2001/07/09 15:41:33 UTC

streaming pdfs through servlet...

This is my first post to this list.  Hi.

I've written a quick little servlet to stream pdf files from my database to
the browser but I'm encountering a strange little problem.  The first time I
call the servlet the page loads (I can see adobe's little plugin
initializing) but nothing ever displays.  All I see is a whitespace in the
browser.  But if I then immediately hit the refresh button then the pdf
shows up.  Has anyone ever seen anything like this before?  I'm not even
sure where the problem lays, i.e. with the plugin, with the browser, with
tomcat, or with my servlet.

Adobe plugin version 5
IE version 5.5

Thanks,
Chris


ps. Here's my servlet.

package com.chessys.trecs;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class ReportManager extends HttpServlet {
  private static final String CONTENT_TYPE = "application/pdf";
  /**Initialize global variables*/
  public void init() throws ServletException {
  }
  /**Process the HTTP Get request*/
  public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    ServletOutputStream out = response.getOutputStream();

    Connection dbConnection = (Connection) request.getSession(
false ).getAttribute( "chesapeake_database_connection");

    try {
      Statement stmt = dbConnection.createStatement();

      ResultSet rs = stmt.executeQuery("Select PDFImage from reportsinpdf
where reportid = " + request.getParameter("id"));

      if (rs.next()) {
        byte [] pdf = rs.getBytes(1);
        out.write(pdf);
        out.flush();
      }

    } catch (Exception e) {
      //Yeah i should probably do something here
    };

    out.close();
  }

  public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
    doGet(request,response);
  }
  /**Clean up resources*/
  public void destroy() {
  }
}


Parse error, missing : in oundary

Posted by Kaneda K <ka...@dedaletechnology.com>.
   Hello, everyone , I have an excption occurde


Parse error, missing : in  oundary=_OPERAB__-aZttkI3ImLNly+tbTRW4wc

Full  POST /capimmo/my/popup/BienSubmitImage.jsp?name=test001&idBien=11 
HTTP/1.1
User-Agent: Opera/5.11 (Windows 2000; U)  [en]Host: 
192.168.111.20:8001Accept: text/html, image/png, image/jpeg, image/gif, 
image/x-xbitmap, */*Accept-Language: en,frAccept-Encoding: deflate, gzip, 
x-gzip, identity, *;q=0Referer: 
http://192.168.111.20:8001/capimmo/my/popup/BienSubmitImage.jsp?name=test001&idBien=11Cookie: 
$Version=1; JSESSIONID=timn93s2b4; $Path="/capimmo"Connection: Keep-Alive, 
TETE: deflate, gzip, chunked, identity, trailersContent-length: 
24366Content-Type: multipart/form-data; 
boundary=_OPERAB__-aZttkI3ImLNly+tbTRW4wc


I don't undersatnd what this error means.

I is supposed to allows some user to store images on the server.

I use for that the O'reilley packadge.

could any one help me with that ?
the strange is that It happen to works correctly before, at least for 200 
times. (that the number of files already store.

...


------------------
System Info :
Red Hat Linux release 6.2 (Zoot)
     Kernel 2.2.14-5.0 on an i686
Mysql Ver 11.12 Distrib 3.23.32
java version "1.3.0"
     Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
     Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)
JDBC Driver : mm.mysql-2.0.4-bin.jar
Tomcat 3.2.2




Re: streaming pdfs through servlet...

Posted by Peter Hrastnik <pe...@1012surf.net>.
I had a similar problem in a php project in the past. There are
browser/acrobat-plugin combinations that cause strange behaviour when
passing through a pdf stream. 
Try using some other browser/acrobat combinations to track your problem
down. If another combination works, you know that your jsp code is
basically ok.

Bye,
	Peter.


-- 
Mag. Peter Hrastnik
tele.ring Telekom Service GmbH
A-1030 Wien, Hainburgerstr. 33
Tel.: +43/1/931012/3277, Mobil: +43/650/6503277



Chris Gross wrote:
> 
> This is my first post to this list.  Hi.
> 
> I've written a quick little servlet to stream pdf files from my database to
> the browser but I'm encountering a strange little problem.  The first time I
> call the servlet the page loads (I can see adobe's little plugin
> initializing) but nothing ever displays.  All I see is a whitespace in the
> browser.  But if I then immediately hit the refresh button then the pdf
> shows up.  Has anyone ever seen anything like this before?  I'm not even
> sure where the problem lays, i.e. with the plugin, with the browser, with
> tomcat, or with my servlet.
> 
> Adobe plugin version 5
> IE version 5.5
> 
> Thanks,
> Chris
> 
> ps. Here's my servlet.
> 
> package com.chessys.trecs;
> 
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.*;
> import java.util.*;
> import java.sql.*;
> 
> public class ReportManager extends HttpServlet {
>   private static final String CONTENT_TYPE = "application/pdf";
>   /**Initialize global variables*/
>   public void init() throws ServletException {
>   }
>   /**Process the HTTP Get request*/
>   public void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>     response.setContentType(CONTENT_TYPE);
>     ServletOutputStream out = response.getOutputStream();
> 
>     Connection dbConnection = (Connection) request.getSession(
> false ).getAttribute( "chesapeake_database_connection");
> 
>     try {
>       Statement stmt = dbConnection.createStatement();
> 
>       ResultSet rs = stmt.executeQuery("Select PDFImage from reportsinpdf
> where reportid = " + request.getParameter("id"));
> 
>       if (rs.next()) {
>         byte [] pdf = rs.getBytes(1);
>         out.write(pdf);
>         out.flush();
>       }
> 
>     } catch (Exception e) {
>       //Yeah i should probably do something here
>     };
> 
>     out.close();
>   }
> 
>   public void doPost(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>     doGet(request,response);
>   }
>   /**Clean up resources*/
>   public void destroy() {
>   }
> }

RE: streaming pdfs through servlet...

Posted by Chris Gross <cg...@chessys.com>.
Thanks for everyones help, I finally found the solution.  I figured I send
it back to the list in case anyone else encounters this same problem in the
future.  I had to set the content length before writing the bytes.  So I
just had to add:

response.setContentLength(pdf.length);

to the servlet before writing any data.

-chris


RE: streaming pdfs through servlet...

Posted by Deacon Marcus <de...@wwtech.pl>.
Hi,
Seems you send it in one big piece, leaving buffering entirely to the
container. Try writing 2048 (or whatever) bytes, flushing, writing next
2048-bytes piece, flushing, etc until the end of file, should help, at least
that's how I do it in similar situations and encountered no problems so far.

Greetings, deacon Marcus

> -----Original Message-----
> From: Chris Gross [mailto:cgross@chessys.com]
> Sent: Monday, July 09, 2001 3:42 PM
> To: tomcatlist
> Subject: streaming pdfs through servlet...
>
>
>
> This is my first post to this list.  Hi.
>
> I've written a quick little servlet to stream pdf files from my
> database to
> the browser but I'm encountering a strange little problem.  The
> first time I
> call the servlet the page loads (I can see adobe's little plugin
> initializing) but nothing ever displays.  All I see is a whitespace in the
> browser.  But if I then immediately hit the refresh button then the pdf
> shows up.  Has anyone ever seen anything like this before?  I'm not even
> sure where the problem lays, i.e. with the plugin, with the browser, with
> tomcat, or with my servlet.
>
> Adobe plugin version 5
> IE version 5.5
>
> Thanks,
> Chris
>
>
> ps. Here's my servlet.
>
> package com.chessys.trecs;
>
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.*;
> import java.util.*;
> import java.sql.*;
>
> public class ReportManager extends HttpServlet {
>   private static final String CONTENT_TYPE = "application/pdf";
>   /**Initialize global variables*/
>   public void init() throws ServletException {
>   }
>   /**Process the HTTP Get request*/
>   public void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>     response.setContentType(CONTENT_TYPE);
>     ServletOutputStream out = response.getOutputStream();
>
>     Connection dbConnection = (Connection) request.getSession(
> false ).getAttribute( "chesapeake_database_connection");
>
>     try {
>       Statement stmt = dbConnection.createStatement();
>
>       ResultSet rs = stmt.executeQuery("Select PDFImage from reportsinpdf
> where reportid = " + request.getParameter("id"));
>
>       if (rs.next()) {
>         byte [] pdf = rs.getBytes(1);
>         out.write(pdf);
>         out.flush();
>       }
>
>     } catch (Exception e) {
>       //Yeah i should probably do something here
>     };
>
>     out.close();
>   }
>
>   public void doPost(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>     doGet(request,response);
>   }
>   /**Clean up resources*/
>   public void destroy() {
>   }
> }