Link to home
Create AccountLog in
Avatar of energie
energie

asked on

Download Servlet with dynamic content

Hi All,

I am have some dynamic content that gets generated from a jsp page, which I want to convert to csv or xml for users to download

I am trying to:
1- gather parameters and construct my content
2- put it in the a bytearray?
3- put this bytearray into my session variable ?
4- forward to my Download Servlet
5- download serlvet will pick up this session variable
6- stream it back to the client

is this the right approach?

I am using the download servlet suggested in this thread:
https://www.experts-exchange.com/questions/20842012/How-to-show-file-download-dialog-box-in-IE-6-0.html

I don't know how to do step 2, 3, and 4 yet :(

here are my questions:

a-  i have this huge data set that could yield huge csv and xml files.  I am not able to create the bytearray ahead of time because I dont know the size, is this a common problem?

b- if bytearray is the right way to go, what is the best way to convert that into a inputstream to feed the outputstream to the user?

c- In the solution from the link, it's works if the file is in the filesystem.  Can this work with dynamic content?.?

thanks very much in advance
E.
Avatar of MicheleMarcon
MicheleMarcon
Flag of Italy image

a & b: If you have an idea for a boundary you can use an array of that size; othewise you could use Vector or some other dynamical structure.
>> a-  i have this huge data set that could yield huge csv and xml files.

You have to really, think here.. How huge it can be.. ?
It's size should never reach to max of heap size.. otherwise out of memory problems will occur..

>>b- if bytearray is the right way to go, what is the best way to convert that into a inputstream to feed the outputstream to the user?

You can write the bytearray directly to the output stream "or"

Create a BufferedInputStream from ByteArrayInputStream (Which in turn should be created from byte array), read the contents from the Buffered Input Stream and write to output stream..

>> c- In the solution from the link, it's works if the file is in the filesystem.  Can this work with dynamic content?.?

Yes.. you can write any thing into the output stream...



Avatar of CEHJ
It's going to be easier to write CSV than XML, since it's easier to stream the former. If you've got a data set (let's call it a ResultSet) then it would be appropriate to pass the RS in the session variable, i.e. no need for a stream.

Make sure you set the content type in the response. You won't be able to set the content length for obvious reasons
Avatar of energie
energie

ASKER

MicheleMarcon:
I won't know..

cmalakar:

it's from a database which could have hundred thousands of row..

because of this reason, It probably sounds like having the bufferedinputstream (created from bytearrayinputstream, which is created from the bytearray) is the best option.

would you happen to have any sample codes?

CEHJ,
by the end of step 1, i already have a blob of text constructed (it is either in csv or xml format) Currently for testing purposes i am writing everything to standard out (system.out) since i wrote my function in my jsp page to write to an outputstream.
>>by the end of step 1, i already have a blob of text constructed (it is either in csv or xml format)

That's not efficient if you're holding much in memory that need to be held
Thats not an efficient way of doing it. you should reorganise it so that you can stream your content directly instead of putting it all into a byte array.
For example you could pass your servlet output stream to the code that generates the content and it could write the content directly to the stream.
Alternatively have a method that returns an input stream that you could plug into your eisting servlet:

                 in = getConstentStream();
              if( in != null ) {
                        out = new BufferedOutputStream( response.getOutputStream() );
                        String contentType = "application/unknow";
                        System.out.println( "contentType: " + contentType );
                        response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
                        int c; while( ( c=in.read() ) != -1 ) out.write( c );

>> it's from a database which could have hundred thousands of row..

Instead of reading all data at once, It is better to , Repeat the following process...

Read some part of data from database, frame the cotent, write it to stream..

>>
Thats not an efficient way of doing it. you should reorganise it so that you can stream your content directly instead of putting it all into a byte array.
>>

Haven't i already said that?
Avatar of energie

ASKER

Thanks all

I do realize that it's not an efficient way of doing it.   But here's my problem, i have the jsp which collects the information and prepare it.  I have the servlet which streams it to the user.

The only way i know how to share infomration between these two is through storing content into a session variable in the jsp, and forwarding to the servlet.  

if it is possible/easy to grab the inputstream inside the servlet from my JSP, that would be the ideal.  Because it avoids storing these tihngs in memory right?  Can this be done?
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account