Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use Servelet to pass binary data

 
bujin wang
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In my application, we need to write both binary and ascii data to the browser. On the client side, our javascript will take advantage of the binary file to do some rendering. The binary data mainly is dynamic image generated on the server side.
In order to achieve that, we have to use HttpServeletResponse.getOutputStream(), which we have already got a binary output stream and I then can hook up a writer = new OutputStreamWriter(this.getOutputStream()) to write string data.
The problem is that our client side has to use JSP, in which a getWriter() is already called during servlet compilation time. That prevents us from call getOutputStream() on the same response. I got IllegalStateException saying that the getOutputStream() is already called.
One possible solution is to use servlet for OutputStream purpose, and have JSP page dispatch the request to the servlet. Here is my concern: does the dispatched response have the OutputStream available to use? Does the getWriter affect the dispatched resquest? How do we call the servlet from JSP page?
Burt
 
bujin wang
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just had new findings here. Even though I don't use JSP pages, just servlet, it still throw exactly the same exception.

Anybody got idea?
bujin
 
Sanjeev Kaushik
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can process in two requests.
One request(for text html contents) will be handled by jsp. And other request for the image url would be handled by serlvet.
Here is the piece of code
I hope this can help you.
 
Sanjeev Kaushik
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this link can also help you
http://www.informit.com/content/index.asp?product_id=%7B99DF9F20-3C8F-4005-AAA7-72D8F25C7EC3%7D
 
bujin wang
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My case is a little bit more complicated. I have to write asscii string(html) to the response as well as binary data(image data).
I wrapped the HttpServletResponse class and use getOutputStream() to grab the stream. If anything catches your eye, please let me know.

The public void write(String s) method can be used to write html strings. However somewhere I got IllegalException which said that the getOutputStream is already called in Tomcat log file, not in the console. The functionality looks normal though.
I have tried to close the stream every time we done writing, however I got try-to-write to a closed stream exception. I have also tried to cache the httpSevletResponse and Response object(such that only one wrapper instance is used for one httpServletResponse object), however it won't work.
Please help!
Bujin Wang
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My case is a little bit more complicated. I have to write asscii string(html) to the response as well as binary data(image data).

Any given response can only write to a character stream or a byte stream. Of course you can send ASCII characters in the byte stream. You should only try substituting your own version of HttpServletResponse if there is no simpler way to accomplish the task.
Frequently new programmers get confused about what is going on when a browser displays characters and one or more images. Remember that a web page can be built up of many separate requests. I suggest you create a static web page that looks like what you want, then figure out how to get one or more servlets to provide the various responses.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic