It's not a secret anymore!
The moose likes Servlets and the fly likes Is there a solution? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is there a solution?" Watch "Is there a solution?" New topic

Is there a solution?

Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Hi all,

My problem is I want to write the character text as well as the binary data to the client in a single servlet.

I have the images in the database.I want to organize the image with in a table and then send to the client.For writing binary data to the client we need ServletOutputStream object which can be obtained by calling response.getOutputStream() method,moreover the content type should be set as either

Then how to send the html to the client i.e the HTML code required to organize the binary data with in a table.For this we need to set the content type as response.setContentType("text/html") and a PrintWriter object is needed to write the character text.If I add the follwing code

with in the same servlet in which the binary data is sent to the client automatically the error occurs.

So what is the solution?

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The browser will send two requests, one for the image and another for the text. You CANNOT send both an image and text in the same response.
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
Thanks Mr.David O'Meara for your suggesstions.I know both binary data and text cannot be sent in a single response,so I asked for the alternative.It will be helpful if you give a small example of how to make the browser to make two requests one for image and another for text.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63867

The same way that you would in any other case, with <img> tags in the HTML.

[Asking smart questions] [About Bear] [Books by Bear]
Arun Somasundaram
Ranch Hand

Joined: Jul 25, 2006
Posts: 63
My browser does not renders the image.Here is the code:-

I can Only see the blank screen.

http://sun/profile/picture is the URL for the servlet which retrieves the image from the database and writes the binary data to the client.
[ September 05, 2006: Message edited by: Arun Somasundaram ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Are you certain that whatever the server sends back is a valid image? Have you tried accessing the image URL directly, saving the result to a file, and then viewing it?

Also note that you're talking about http://sun/profile/picture, while the code uses http://sun/jspbook/picture.
[ September 05, 2006: Message edited by: Ulf Dittmer ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Please see:UseAMeaningfulSubjectLine

"Is there a solution?" tells us nothing about your question.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Is there a solution?
It's not a secret anymore!