• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to retrieve images from mysql database into a portlet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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";
Class.forName(driver).newInstance();

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();

while(rs.next())
{
FileOutputStream fo = new FileOutputStream("/images/theimage.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fo);
bos.write(rs.getBytes("image"));
bos.close();
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
// close the connection so it can be returned to
// the connection pool then return the list
con.close();


}



}

}



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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use

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

See this JSR 168 tips for some good tips
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

encodeURL

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.

Parameters:
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).
Returns:
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
 
and POOF! You're gone! But look, this tiny ad is still here:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic