Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Retrieve Image in JSF From MySQL Table Using NetBeans IDE

In this application we create a jsp page to display image.


JSF Tutorials: Easy steps to learn JSF Java Server Faces (JSF) Tutorials What is JSF Mojarra? JSF Life Cycle JSF Examples JSF Tutorial For Beginners Roseindia JSF Tutorial JSF core Tag Reference JSF internationalization example Tomahawk selectOneCountry tag RCFaces About Hibernate, Spring and JSF Integration Tutorial Creating Login Page In JSF using NetBeans JSF Hello World Declaring the Bean class in the faces-config.xml file Creating a Web Application with the JSF Framework Binding the Form Elements With the Bean Properties Adding Navigation in JSF Application Binding Component Instance to a Bean Property Tomahawk validateEqual tag Tomahawk validateEmail tag Tomahawk validateCreditCard tag Tomahawk selectOneListbox tag Tomahawk selectManyListbox tag Tomahawk selectManyCheckbox tag Tomahawk selectBooleanCheckbox tag Tomahawk saveState tag Tomahawk popup tag Tomahawk inputCalendar tag Tomahawk htmlTag tag Tomahawk graphicImage tag Tomahawk documentHead tag Tomahawk documentBody tag Tomahawk document tag Tomahawk column tag DataBase Connectivity with MySql in Visual Web JSF Application Using Net Beans IDE Retrieve Image in JSF From MySQL Table Using NetBeans IDE JSF Training Java Sever Faces 1.2 - JSF 1.2 Display Data from Database in JSF Application Business Objects of Business Logic tier Setup MySQL Database Working with Database through JDBC Working with JavaScript & CSS Referencing a Localized Message Referencing Resource Bundle from a Page Binding Component Value to an Implicit Object Phase Listener Value Change Event Listener Action Event Listener

Retrieve Image in JSF From MySQL Table Using NetBeans IDE


This application illustrates how to retrieve image and data from MySQL table using NetBeans IDE.

In this application we create a jsp page to display image. In this JSP page we are using <h:graphicImage> tag to access the servlet, here we are uses servlet but as u wish u can access a jsp page also. To create the project in the NetBeans IDE select File > click on "Create Project" then a New Project window will be appear, select Web from Categories section and select Web Application from Projects section. Click Next.  

Put Project Name and click Next.

Choose JavaServer Faces and click Finish.

To create the jsp file right click on Web Pages select New and click JSP as below:


Here is the Source Code of data.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri=""%>
<%@taglib prefix="h" uri=""%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

  <head><h2>JSF Application</h2></head>
  <h:dataTable id="dt1" value="#{tableBean.perInfoAll}" 
"item" bgcolor="#F1F1F1" border="10" cellpadding="5" 
"3" rows="4" width="50%">
  <f:facet name="header">
  <h:outputText value="This is Table Data" />
  <f:facet name="header">
  <h:outputText value="Emp Name" />
  <h:outputText value="#{}"/>  

  <f:facet name="header">
  <h:outputText value="Image" />
  <h:graphicImage value="DisplayImage"/> 

  <f:facet name="footer">
  <h:outputText value="The End" />

 To create the package right click on Source Package and select New > Java Package..

The following window will appear then put package name and click Finish.


To create the java class right click on newly created package and select New > Java Class

Put the Class Name and click Finish.

Source Code of 

package roseindia;

import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayImage extends  HttpServlet{
  public void doGet(HttpServletRequest request, 
HttpServletResponse response

ServletException, IOException{
  String connectionURL = "jdbc:mysql://localhost:3306/test";
    java.sql.Connection con=null;
  Statement st1;
  con=DriverManager.getConnection   (connectionURL,"root","root");  
  ResultSet rs1 = st1.executeQuery

"select image from pictures where id = '2'");
  String imgLen="";
  imgLen = rs1.getString(1);
  int len = imgLen.length();
  byte [] rb = new byte[len];
  InputStream readImg = rs1.getBinaryStream(1);
  int, 0, len);  
 catch (Exception e){

 Create Bean class to set and get the table column as below the source code of 

package roseindia;

import java.sql.*;
import java.util.*;

public class TableBean {
  Connection con ;
  Statement ps;
  ResultSet rs;
  private List perInfoAll = new ArrayList();

  public void setPerInfoAll(List perInfoAll) {
  this.perInfoAll = perInfoAll;
  public List getPerInfoAll() {
  int i = 0;
 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/
 ps = con.createStatement();
 rs = ps.executeQuery("select name from emp");
 perInfoAll.add(i,new tableData(rs.getString("name")));
  catch (Exception e) {
  System.out.println("Error Data : " + e.getMessage());
  return perInfoAll;
  public class tableData {
  private String name;
  public tableData(String name) { = name;

  public void setName(String name) { = name;
  public String getName() {
  System.out.println("Name: "+name);
  return name;


Servlet Mapping in web.xml



Source Code of faces-config.xml 

<?xml version='1.0' encoding='UTF-8'?>

<!-- =========== FULL CONFIGURATION FILE ================== -->

<faces-config version="1.2" 
  xsi:schemaLocation=" http://j"



The Output shown on the Browser as below:

Download Complete Source Code


Share on Google+Share on Google+

Retrieve Image in JSF From MySQL Table Using NetBeans IDE

Posted on: June 9, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Retrieve Image in JSF From MySQL Table Using NetBeans IDE  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
January 11, 2012
nt working

the servlet is showing the image bt in the JSF images arent getting displayed :( help me... i used the same code..
Kemal Duran
July 15, 2013
datatable showing many images

it's working but, it gets only one image from database like this : ("select image from pictures where id = '2'"); I have many images to show datatable What can I do?
April 3, 2013

i used this same code but it only index.xhtml alone after that other page are display not running.................. how to run this program please help me