Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to write binary data into a JSP output?

Reply
Thread Tools

How to write binary data into a JSP output?

 
 
Joona I Palaste
Guest
Posts: n/a
 
      08-21-2003
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 ((E-Mail Removed)) ---------------------------\
| 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
 
Reply With Quote
 
 
 
 
Marko Lahma
Guest
Posts: n/a
 
      08-21-2003

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

 
Reply With Quote
 
 
 
 
Dave Miller
Guest
Posts: n/a
 
      08-21-2003
In article <bi2q4f$t8m$(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      08-21-2003
Dave Miller <(E-Mail Removed)> scribbled the following:
> In article <bi2q4f$t8m$(E-Mail Removed)>,
> (E-Mail Removed) 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 ((E-Mail Removed)) ---------------------------\
| 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
 
Reply With Quote
 
Michiel Konstapel
Guest
Posts: n/a
 
      08-21-2003
"Joona I Palaste" <(E-Mail Removed)> wrote in message
news:bi2q4f$t8m$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Wolfram Rittmeyer
Guest
Posts: n/a
 
      08-21-2003
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




 
Reply With Quote
 
Wolfram Rittmeyer
Guest
Posts: n/a
 
      08-21-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP newbie - can I use JSP to access exiting java classes withoutre-write to javabeans terry433iid@yahoo.com Java 2 03-17-2008 01:40 AM
[JSP] difference between jsp:forward and jsp:include alexjaquet@gmail.com Java 0 06-02-2006 01:21 PM
jsp:include is not allowing me write scriptlet between <jsp:param> tags ramakrishna Java 0 05-04-2006 12:49 PM
Question about reading a big binary file and write it into several text (ascii) files Albert Tu Python 2 01-25-2005 03:56 AM
how to write jsp custom tag that can have all kind of jsp elements(not only other tags but also srciplets) in his body Lukasz Indyk Java 1 09-22-2004 12:43 PM



Advertisments