JSF Event Handling and Database Access

Posted By on April 30, 2016

Download PDF
JSF Facelets Tag
JSF Libraries: PrimeFaces

JSF Event Handling

  • When a user clicks a JSF button or link or changes any value in text field, JSF UI component fires event which will be handled by the the application code.
  • To handle such event, event handler are to be registered in the application code or managed bean.
  • When a UI component checks that a user event has happened, it creates an instance of the corresponding event class and adds it to an event list.
  • Then, Component fires the event, i.e., checks the list of listeners for that event and call the event notification method on each listener or handler.


JSF also provide system level event handlers which can be used to do some tasks when application starts or is stopping.

Following are important Event Handler in JSF 2.0:

S.N. Event Handlers & Description
1 valueChangeListener

Value change events get fired when user make changes in input components.

2 actionListener

Action events get fired when user clicks on a button or link component.

3 Application Events

Events firing during JSF lifecycle: PostConstructApplicationEvent, PreDestroyApplicationEvent , PreRenderViewEvent.


JSF Database Access

Database requirements to run this example

S.N. Software & Description
1 PostgreSQL 9.1

Open Source and light weight database

2 PostgreSQL JDBC4 Driver

JDBC driver for PostgreSQL 9.1 and JDK 1.5 or above

Put PostgreSQL JDBC4 Driver jar in tomcat web server’s lib directory

Database SQL Commands

create user user1;

create database testdb with owner=user1;

    id int PRIMARY KEY, 
    name VARCHAR(25)

INSERT INTO authors(id, name) VALUES(1, 'Rob Bal');
INSERT INTO authors(id, name) VALUES(2, 'John Carter');
INSERT INTO authors(id, name) VALUES(3, 'Chris London');
INSERT INTO authors(id, name) VALUES(4, 'Truman De Bal');
INSERT INTO authors(id, name) VALUES(5, 'Emile Capote');
INSERT INTO authors(id, name) VALUES(7, 'Breech Jabber');
INSERT INTO authors(id, name) VALUES(8, 'Bob Carter');
INSERT INTO authors(id, name) VALUES(9, 'Nelson Mand');
INSERT INTO authors(id, name) VALUES(10, 'Tennant Mark');

alter user user1 with password 'user1';

grant all on authors to user1;

Example Application

Let us create a test JSF application to test jdbc integration.

Step Description
1 Create a project with a name helloworld under a packagecom.tutorialspoint.test as explained in the JSF – First Application chapter.
2 Create resources folder under src > main folder.
3 Create css folder under src > main > resources folder.
4 Create styles.css file under src > main > resources > css folder.
5 Modify styles.css file as explained below.
6 Modify pom.xml as explained below.
7 Create Author.java under package com.tutorialspoint.test as explained below.
8 Create UserData.java under package com.tutorialspoint.test as explained below.
9 Modify home.xhtml as explained below. Keep rest of the files unchanged.
10 Compile and run the application to make sure business logic is working as per the requirements.
11 Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
12 Launch your web application using appropriate URL as explained below in the last step.


   border-bottom:1px solid #000000;

   background:none repeat scroll 0 0 #B5B5B5;
   border-bottom:1px solid #000000;
   border-top:1px solid #000000;

   background:none repeat scroll 0 0 #FFFFFFF;	

   background:none repeat scroll 0 0 #D3D3D3;


<project xmlns="http://maven.apache.org/POM/4.0.0" 
   <name>helloworld Maven Webapp</name>


package com.tutorialspoint.test;

public class Author {
   int id;
   String name;
   public String getName() {
      return name;
   public void setName(String name) {
      this.name = name;
   public int getId() {
      return id;
   public void setId(int id) {
      this.id = id;


package com.tutorialspoint.test;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ComponentSystemEvent;

@ManagedBean(name = "userData", eager = true)
public class UserData implements Serializable {

   private static final long serialVersionUID = 1L;

   public List<Author> getAuthors(){
      ResultSet rs = null;
      PreparedStatement pst = null;
      Connection con = getConnection();
      String stm = "Select * from authors";
      List<Author> records = new ArrayList<Author>();
      try {   
         pst = con.prepareStatement(stm);
         rs = pst.getResultSet();

            Author author = new Author();
      } catch (SQLException e) {
      return records;

   public Connection getConnection(){
      Connection con = null;

      String url = "jdbc:postgresql://localhost/testdb";
      String user = "user1";
      String password = "user1";
      try {
         con = DriverManager.getConnection(url, user, password);
         System.out.println("Connection completed.");
      } catch (SQLException ex) {
      return con;


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="http://www.w3.org/1999/xhtml"
      <title>JSF Tutorial!</title>
      <h:outputStylesheet library="css" name="styles.css"  /> 
   <h2>JDBC Integration Example</h2>
   <h:dataTable value="#{userData.authors}" var="c"
      <h:column><f:facet name="header">Author ID</f:facet>
      <h:column><f:facet name="header">Name</f:facet>

Once you are ready with all the changes done, let us compile and run the application as we did in JSF – First Application chapter. If everything is fine with your application, this will produce following result:


JSF Facelets Tag
JSF Libraries: PrimeFaces

Download PDF