File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Portals and Portlets and the fly likes How to retrieve images from mysql database into a portlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Portals and Portlets
Bookmark "How to retrieve images from mysql database into a portlet" Watch "How to retrieve images from mysql database into a portlet" New topic

How to retrieve images from mysql database into a portlet

mohan kondapalli

Joined: May 02, 2007
Posts: 2
How to retreive images from mysql database into the portlet

i have used following code

package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class AccessDBI

// create SQL strings to query database tables

// get subject names from database
public void getImage() throws Exception
String driver = "com.mysql.jdbc.Driver";

Connection con = null;

try {
String url = "jdbc:mysql://localhost/test";
con = DriverManager.getConnection(url,"root","");
// prepare the SQL query to get subject name and id
PreparedStatement pst=con.prepareStatement("select image from images");

ResultSet rs = pst.executeQuery();

FileOutputStream fo = new FileOutputStream("/images/theimage.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fo);
catch(Exception e)
// close the connection so it can be returned to
// the connection pool then return the list




and in the view.jsp

i have img src="/images/theimage.jpg"

yet i am unable to display the images

(question 2)

if i need to diplay the image as in the conventional way of jsp(WEB INF) where can i store the image so that to display in the protlet
Vikrant Pandit
Ranch Hand

Joined: Mar 27, 2006
Posts: 245

PortletResponse.encodeURL() to encode your image path in the jsp .

See this JSR 168 tips for some good tips

Vikrant Pandit
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Since the portal uses a crazy, internal URL to identify war files, and content roots of those war files, relative paths aren't as useful as they are in Servlet and JSP based apploications. Use the encoreURL method of the PortletResponse:


public java.lang.String encodeURL(java.lang.String path)

Returns the encoded URL of the resource, like servlets, JSPs, images and other static files, at the given path.

Some portal/portlet-container implementation may require those URLs to contain implementation specific data encoded in it. Because of that, portlets should use this method to create such URLs.

The encodeURL method may include the session ID and other portal/portlet-container specific information into the URL. If encoding is not needed, it returns the URL unchanged.

path - the URI path to the resource. This must be either an absolute URL (e.g. or a full path URI (e.g. /myfolder/myresource.gif).
the encoded resource URL as string

From the Portlet API

Here's a few more portlet best practices if you're interested:

Best Practices for Architecting a Portal Solution
I agree. Here's the link:
subject: How to retrieve images from mysql database into a portlet
It's not a secret anymore!