Best approach? Read BLOB .JPG from database into browser

Can anyone advise on the best approach to reading a JPEG image from a database BLOB field in IS and returning that to be displayed in a CAF application?

I would ideally like a way to do this without writing the file out to disk, i.e. return the byte data for the image and then render that back to the browser as a regular JPG.

I tried creating a regular J2EE servlet to do this but it doesn’t seem like I can change web.xml to map this servlet in a portlet application.

(A similar question was asked in an internal email list so i’ve shamelessly copied the response from an expert)

The SDK site has a sample that demonstrates how to stream a file back from a JSF action.

See http://ajax-softwareag.com/articles/6Y3MUW/MywebM_Samples_doc/MywebM_Samples_FileExport.html

You should be able to combine that technique with an Image control in your view to display a streamed image in your UI. You would simply need to build up the portlet action URL that would invoke the action and bind it to the Image control value.

For example, you may add something like these two java methods to your page bean.

/**
 * Use this custom method to build the action url
 * that you can bind to the Image control value. 
 */
public String getDynamicImageURL() {
	try {
		IPortletURL actionUrl = createActionUrl();
		//set the target action to an expression that resolves to the JSF action
		actionUrl.setTargetAction("#{activePageBean.doStreamImage}");
		
		return actionUrl.toString();
	} catch (Exception e) {
		error(e);
	}
	return null;
}

/**
 * Use this custom action method to retrieve the image data and stream it back
 */
public String doStreamImage() {
	IFileExportBean fileExportBean = null;
	//TODO: build your file export bean from the data from your web service.  See the SDK sample for examples.
	
	streamFileDataToResponse(fileExportBean);
	return OUTCOME_OK;
}