*
The moose likes Servlets and the fly likes How to use Servelet to pass binary data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to use Servelet to pass binary data" Watch "How to use Servelet to pass binary data" New topic
Author

How to use Servelet to pass binary data

bujin wang
Ranch Hand

Joined: Jun 21, 2003
Posts: 48
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

Joined: Jun 21, 2003
Posts: 48
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

Joined: Aug 01, 2002
Posts: 105
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
Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
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

Joined: Jun 21, 2003
Posts: 48
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

Joined: Mar 22, 2000
Posts: 12760
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use Servelet to pass binary data
 
Similar Threads
OutputStream in Jsp
See this code it gives exception
Response filter..
Doubt on RequestDispatcher
OutputStream irritates