• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Converting ServletOutputStream in jsp to bytes

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically my task is to create/download an excel file on click of a button and also to send it to another Api.
Creating excel is not an issue, problem is I don’t understand how to store this get bytes of this excel to send further to Api.
I am restricted, I cannot use third party jars like apache Poi etc.
So currently I am simply using a jsp.

On click of a button I am calling a 2nd jsp

1 jsp has something like this
generateExcel( ) {
Location.href=“some path for second jsp”;
}

2 jsp has table structure which will display in excel
Besides that have added following in response to make it download as excel

response.setContentType("application/xls");
               response.setHeader("Content-Disposition", "attachment; filename=" + "new.xls");

So 2nd jsp has 2 parts 1 is html which shows inside excel and the response header setting part which tells browser to download this jsp as excel.

Now my problem is how to send this excel to Api. Is it possible to get bytes/or base64 string from servletOutputStream before download.
I want Something like
response.getOutPutStream().getBytes// how to make this work
 
Sheriff
Posts: 28331
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answer is that you can't get data out of an OutputStream in Java. Don't put data into an OutputStream if you need to process it later.

As for anything else helpful, unfortunately I can't give you that. I don't really know what you're doing so I can't suggest what else you can do.

You have two JSPs? If you're producing Excel data for download you shouldn't be using any JSPs at all. That's a job for servlets. And anyway I don't understand why clicking a button (presumably the user clicks a button in the browser) should start the second JSP. What happened to the first one?

And you said "Location.href=“some path for second jsp”". This looks like JavaScript, so what is it doing here?
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of things. First and foremost, very much agree with Paul. JSP is absolutely the wrong tool for this job. JSP is a templating language meant to generate text output, usually HTML. As that's not what you are doing here, you are thinking of using a hammer to drive a screw.

Moreover, putting Java code in a JSP has been obsolete for 21 years. 21. Years.

You say you want to pass the Excel bytes to an API, but that's not what you are describing here. You are describing sending it as the response to the request. Which is it? Calling an API, or sending a response?

If a response is actually what you need, and you have the bytes of the Excel handy (which you said was not a problem), then you've already set the headers of the response, you just need to write the bytes to the response. Not sure why you think you need to get something from the response. Quite the opposite, you send information as the response.
 
J Kj
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand it is a weird way, but it is an old project, and I am restricted.I cannot be adding new things to it. So poi jars are not possible.
As per this projects structure there is 1 servlet only which i cannot edit.
Thats why was trying this jsp way. I don’t fully understand it but my thinking was before jsp outputs response i may be able to save this jsp or get bytes out of it. Now I understand it is not possible. Thanks for your reply. I will check if possible to update this project
 
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic