?
Solved

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

Posted on 2005-03-31
14
Medium Priority
?
276 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
  • 7
  • 3
  • 3
13 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

This installment of Make It Better gives Media Temple customers the latest news, plugins, and tutorials to make their Grid shared hosting experience that much smoother.
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

616 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