How to write binary data into a JSP output?

Discussion in 'Java' started by Joona I Palaste, Aug 21, 2003.

  1. We're currently stuck in our JSP application. We need to output a PDF
    file from a JSP page. However, it turns out that the only way to write
    anything to the JSP page's output is to use a
    javax.servlet.jsp.JspWriter, which does not correctly handle raw binary
    data (necessary for writing PDFs). Its mere existence prevents us from
    even getting the output stream directly, much less writing binary data
    to it.
    Is there any way in the world that we could write raw binary data,
    specifically, PDFs, to a JSP page output? The data is dynamically
    generated, and can come from a java.lang.String or a byte array.
    Reading it from a file would not be such a good idea because we would
    get hassles with temporary files.
    Please help, thanks.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "No, Maggie, not Aztec, Olmec! Ol-mec!"
    - Lisa Simpson
     
    Joona I Palaste, Aug 21, 2003
    #1
    1. Advertising

  2. Joona I Palaste

    Marko Lahma Guest

    Remember that everything doesn't need to be done with JSPs. Just do it
    with good ol' Servlets and problem solved. JSPs just don't fit to every
    situation.

    -Marko
     
    Marko Lahma, Aug 21, 2003
    #2
    1. Advertising

  3. Joona I Palaste

    Dave Miller Guest

    In article <bi2q4f$t8m$>,
    says...
    > We're currently stuck in our JSP application. We need to output a PDF
    > file from a JSP page. However, it turns out that the only way to write
    > anything to the JSP page's output is to use a
    > javax.servlet.jsp.JspWriter, which does not correctly handle raw binary
    > data (necessary for writing PDFs). Its mere existence prevents us from
    > even getting the output stream directly, much less writing binary data
    > to it.
    > Is there any way in the world that we could write raw binary data,
    > specifically, PDFs, to a JSP page output? The data is dynamically
    > generated, and can come from a java.lang.String or a byte array.
    > Reading it from a file would not be such a good idea because we would
    > get hassles with temporary files.
    > Please help, thanks.
    >
    >

    I'm not sure that I understand the question (but)...

    take a look at:

    http://www.lowagie.com/iText/

    his API might help.
     
    Dave Miller, Aug 21, 2003
    #3
  4. Dave Miller <> scribbled the following:
    > In article <bi2q4f$t8m$>,
    > says...
    >> We're currently stuck in our JSP application. We need to output a PDF
    >> file from a JSP page. However, it turns out that the only way to write
    >> anything to the JSP page's output is to use a
    >> javax.servlet.jsp.JspWriter, which does not correctly handle raw binary
    >> data (necessary for writing PDFs). Its mere existence prevents us from
    >> even getting the output stream directly, much less writing binary data
    >> to it.
    >> Is there any way in the world that we could write raw binary data,
    >> specifically, PDFs, to a JSP page output? The data is dynamically
    >> generated, and can come from a java.lang.String or a byte array.
    >> Reading it from a file would not be such a good idea because we would
    >> get hassles with temporary files.
    >> Please help, thanks.
    >>

    > I'm not sure that I understand the question (but)...


    > take a look at:


    > http://www.lowagie.com/iText/


    > his API might help.


    No, I'm sorry, but it seems like you did not understand the question.
    We already have a working way to *generate* PDFs. It's getting them into
    the ServletResponse that's the problem. The sample code on the page you
    mentioned looks like it's using a Servlet directly, which isn't an
    option for us.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "The truth is out there, man! Way out there!"
    - Professor Ashfield
     
    Joona I Palaste, Aug 21, 2003
    #4
  5. "Joona I Palaste" <> wrote in message
    news:bi2q4f$t8m$...
    > We're currently stuck in our JSP application. We need to output a PDF
    > file from a JSP page. However, it turns out that the only way to write
    > anything to the JSP page's output is to use a
    > javax.servlet.jsp.JspWriter, which does not correctly handle raw

    binary
    > data (necessary for writing PDFs). Its mere existence prevents us from
    > even getting the output stream directly, much less writing binary data
    > to it.
    > Is there any way in the world that we could write raw binary data,
    > specifically, PDFs, to a JSP page output? The data is dynamically
    > generated, and can come from a java.lang.String or a byte array.
    > Reading it from a file would not be such a good idea because we would
    > get hassles with temporary files.


    It won't work. As you discovered, all you get is a JspWriter. That
    means, that response.getOutputStream() has already been called (and
    wrapped by the JspWriter), making the raw output stream unavailable
    (text or binary, it's one or the other). JSPs are not meant for binary
    content. Just use a servlet.
    HTH,
    Michiel
     
    Michiel Konstapel, Aug 21, 2003
    #5
  6. Joona I Palaste wrote:

    > We're currently stuck in our JSP application. We need to output a PDF
    > file from a JSP page. However, it turns out that the only way to write
    > anything to the JSP page's output is to use a
    > javax.servlet.jsp.JspWriter, which does not correctly handle raw binary
    > data (necessary for writing PDFs). Its mere existence prevents us from
    > even getting the output stream directly, much less writing binary data
    > to it.


    A JSPWriter is not the way to go. Instead get the normal ServletOutputStream
    using "response.getOutputStream()". You can use this one just as any other
    OutputStream. Be careful though: You mustn't have flushed any content from
    within this JSP to the client prior to this. Also you should set the
    content-type accordingly. So it is possible writing a PDF-file using JSPs.
    Nevertheless for binary data only a servlet is always the better way to go.

    ---
    Wolfram Rittmeyer
    www.jsptutorial.org
     
    Wolfram Rittmeyer, Aug 21, 2003
    #6
  7. Joona I Palaste wrote:
    > That's what I tried, but I got an InvalidStateException
    > ("getOutputStream() has already been called on this response") without
    > using the JspWriter at all. I also took care not to have any
    > <%@include%> statements.
    > Hmm, on second thought, it looks like there's a call to:
    > out.write("\n");
    > at the top of the generated code from the JSP. The JSP page source code
    > looks something like this:
    >
    > <%@page import='com.ourcompany.api.*'%>
    > <% if (!isPDF()) { %>
    > <%@include file="normal.html"%>
    > <% }
    > else {
    > ServletOutputStream out = response.getOutputStream();
    > /* ... */
    > } %>
    >
    > Could the call to out.write("\n") have come from the newline between the
    > <%@page import%> statement and the <% if ()%> statement? If so, then
    > would it work if I just removed the newline?
    >


    Well this - of course - shouldn't happen. With Tomcat this will not happen.
    At least not until you set the buffer to "none". So to be on the safe side
    insert the following page-directive:
    <%@page buffer="16kb">

    If that's still not working you should add some part of your servlet into
    your answer.

    Wolfram Rittmeyer
    http://www.jsptutorial.org
     
    Wolfram Rittmeyer, Aug 21, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page