• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to retrieve images from mysql database into a portlet  RSS feed

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to retreive images from mysql database into the portlet

i have used following code

package com.test;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
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
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

See this JSR 168 tips for some good tips
author and cow tipper
Saloon Keeper
Posts: 4968
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. http://my.co/myportal/mywebap/myfolder/myresource.gif) 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!