?
Solved

Need help displaying images from a mysql database using a servlet of jsp?

Posted on 2005-03-31
14
Medium Priority
?
273 Views
Last Modified: 2010-04-01
Need to display images on my webpage from a mysql database ? Also is it possible to resize them as i want all product images to be a standard size!

Would greatly appreciate any help with the above?
0
Comment
Question by:climbingjaffa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
14 Comments
 
LVL 9

Expert Comment

by:cb1393
ID: 13674537
0
 
LVL 2

Author Comment

by:climbingjaffa
ID: 13674605
Thanks but kinda looking for jsp or servlet example
0
 
LVL 9

Expert Comment

by:cb1393
ID: 13674625
Ah. For future reference, you might want to post in the JSP topic area.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:cb1393
ID: 13674646
0
 
LVL 2

Author Comment

by:climbingjaffa
ID: 13674875
Sorry about that thought i did!  Thanks
0
 
LVL 2

Author Comment

by:climbingjaffa
ID: 13681612
My files are stored as binary blobs in the MYSQL database.... can any one give me a but more information on how to use the code in the link above and does it work for gif's and jpg's?
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13691969
yup thats very doable..
you will have to write a servlet that reads the blob from the MYSQL.. it will look something like..

String keys=request.getParameter("Keys"); // this is the key to your MYSQL table..

//connect to the database
//create a statement.

ResultSet rs=stmt.executeQuery("Select blob from your table where key="+keys);
Blob b=rs.getBlob(1);

InputStream is = b.getInputStream();

response.setContentType("image/gif")
ServletOutputStream os = response.getOutputStream();
 byte[] bt = new byte [1000];
                  for(int i = is.read(bt); i != -1; )
                  {
                     os.write(bt);
                     is.read(bt);
                  }
                  os.flush();
                  os.close();
}

and in your jsp all you have to do is<IMG SRC=/servlet/YourServlet?keys=yourkeys hight=100 width=200/>
0
 
LVL 2

Author Comment

by:climbingjaffa
ID: 13728526
tried the above but just get a red x in the jsp.  Fine if i display directly in the servlet. im using image/jpeg would this have any bearing on it!
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13729113
if your images are of extension jpeg.. then your
response.setContentType("image/gif") should be
response.setContentType("image/jpeg")

and if its displaying directly from Servlet then servlet is working fine.. we are missing something in the <IMG tag of the jsp..
can u post the code of the jsp so that I can have a look at it??
0
 
LVL 2

Author Comment

by:climbingjaffa
ID: 13734006
<TD><IMG SRC="displayimageservlet?picid=<%=productId%>" HEIGHT = "200" WIDTH = "200"</TD>
0
 
LVL 2

Author Comment

by:climbingjaffa
ID: 13734034
I judt out . println in the servlet and it appears fine, yeah it must be with the jsp i have the problem.
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 1500 total points
ID: 13736111
the image tag does not look right to me.. they shd be

<TD><IMG SRC="displayimageservlet?picid=<%=productId%>" HEIGHT = "200" WIDTH = "200"/></TD>

try this
Also post your servlet code so that we can be sure that it works... the easiest test will be that your call your servlet from the browser and it should display the image in the browser

0
 
LVL 2

Author Comment

by:climbingjaffa
ID: 13738855
Yeah my image tag was missing the /. Thanks for your help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New style of hardware planning for Microsoft Exchange server.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question