How to write binary data into a JSP output?

J

Joona I Palaste

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 ([email protected]) ---------------------------\
| 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
 
M

Marko Lahma

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
 
D

Dave Miller

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.
 
J

Joona I Palaste

Dave Miller said:
I'm not sure that I understand the question (but)...
take a look at:

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 ([email protected]) ---------------------------\
| 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
 
M

Michiel Konstapel

Joona I Palaste said:
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
 
W

Wolfram Rittmeyer

Joona said:
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.
 
W

Wolfram Rittmeyer

Joona said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top