MultipartResponse. Servlet generating "normal" web page (pics + text)

P

Paul Smith

Hello,

I am trying to teach myself about JSPs and Servlets. I decided to
create a web application that allows me to enter some search criteria
and display the results in the form of a list of items comprising some
text and a picture (like any of the shopping sites on the web).

I thought I might use the O'Reilly MultipartResponse class as I saw
that you could have several different content types for the same
response, and I guessed I needed text/plain and images/jpeg on the
same page. It turns out that IE does not support multi-part responses,
which made me realise that I must be going down the wrong track using
the MultipartResponse object. After all, I use IE and can use online
shopping sites just fine.

Could someone tell me, at a high level summary level, how such sites
are created. I want to store pictures in a database and retrieve them
and their description in response to a user search and present a list
of the results, with the pictures and descriptions side-by-side, in
the user's browser.

Many thanks,
Paul
 
C

Christophe Vanfleteren

Paul said:
Hello,

I am trying to teach myself about JSPs and Servlets. I decided to
create a web application that allows me to enter some search criteria
and display the results in the form of a list of items comprising some
text and a picture (like any of the shopping sites on the web).

I thought I might use the O'Reilly MultipartResponse class as I saw
that you could have several different content types for the same
response, and I guessed I needed text/plain and images/jpeg on the
same page. It turns out that IE does not support multi-part responses,
which made me realise that I must be going down the wrong track using
the MultipartResponse object. After all, I use IE and can use online
shopping sites just fine.

Could someone tell me, at a high level summary level, how such sites
are created. I want to store pictures in a database and retrieve them
and their description in response to a user search and present a list
of the results, with the pictures and descriptions side-by-side, in
the user's browser.

Many thanks,
Paul

Create html like this:

...
<p>text retrieved from the db</p>
<img src="http://yourhost/image?id=xxx/">
....

Make image a servlet that sets its content-type to image/jpeg, read the image
from the db using the id parameter and write the image out in the response.
 
S

Sudsy

Paul said:
Hello,

I am trying to teach myself about JSPs and Servlets. I decided to
create a web application that allows me to enter some search criteria
and display the results in the form of a list of items comprising some
text and a picture (like any of the shopping sites on the web).

I thought I might use the O'Reilly MultipartResponse class as I saw
that you could have several different content types for the same
response, and I guessed I needed text/plain and images/jpeg on the
same page. It turns out that IE does not support multi-part responses,
which made me realise that I must be going down the wrong track using
the MultipartResponse object. After all, I use IE and can use online
shopping sites just fine.
Many thanks,
Paul

My, my! Go to www.w3c.org and read all about HTML and HTTP. You
don't send all the parts of a page as a response; you include
links to content in your HTML (M stands for "markup" after all)
and it's up to the browser to pull it down.
I don't know how/if you can do this in IE but in Netscape Ctrl-I
displays all the forms, links, media (images and the like) in a
page.
If you don't comprehend the underlying protocol then choosing a
particular class (Mutlipart Response) to solve an undefined
problem seems a bit of a stretch...
YMMV
 
P

Paul Smith

Thank you both for your replies. So if I have images in a database, my
servlet needs to read them from the database, write them out to files,
and then include links to those files in the HTML the servlet
produces?

Paul
 
M

Michael Borgwardt

Paul said:
Thank you both for your replies. So if I have images in a database, my
servlet needs to read them from the database, write them out to files,
and then include links to those files in the HTML the servlet
produces?

Absolutely no. First of all, I would seriously consider removing the images from
the database and having them as files in the first place, because keeping them
in the DB is just a giant performance killer if you don't need the transactional
safety.

In the unlikely case that the images really need to be in the DB, writing them to
files on demand could form a cache, mitigating the performance loss, but it would
also require cache management in order not to keep displaying older versions,
and there would be timing problems.
All in all, it sound like a bigger headache than it's worth. The alternative is
to have the HTML page include links not to file but to another servlet, which
reads the image from the DB and returns it directly as binary data.
 
Joined
May 22, 2012
Messages
1
Reaction score
0
I think you need to step back a bit and just think of JSP pages as plain old HTML pages with a bunch of <% %> scriptlet tags capable fo performing logic on the server side before being delivered to the user.

So if you want to show an image from your photo collection on your JSP page, just write:

<img src="http://www.velocityreviews.com/forums/images/photos/photo_1.jpg" /> .. like usual - with plain HTML.

But if you want a random image from your entire collection of 3000 photos to be displayed whenever the page is opened, instead write:

<img src="http://www.velocityreviews.com/forums/images/photos/photo_<%=new Random().nextInt(3000) + 1 %>.jpg" /> ..

There's a lot more to JSP than Scriptlets of course, but the fun is getting there :) I knew nothing about JSP in December 2010 year ago and now (May 2012) I've got an entire board game running on it. Incidentally --> http://apps.facebook.com/sovereign :wink:

Good luck!
-JS


EDIT: Noticed that this thread is 9 years old - how embarassing... Sure hope everyone's well.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top