Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8103995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:42:35+00:00 2026-06-05T23:42:35+00:00

I am trying to connect the JSF to database. This are the files web.xml

  • 0

I am trying to connect the JSF to database.

This are the files

web.xml

      <?xml version="1.0" encoding="UTF-8"?>
     <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">

     <display-name>JavaServerFaces</display-name>

    <resource-ref>
 <description>MySQL Datasource example</description>
<res-ref-name>jdbc/mkyongdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
   </resource-ref>


   <!-- Change to "Production" when you are ready to deploy -->
   <context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
  </context-param>

  <!-- Welcome page -->
 <welcome-file-list>
  <welcome-file>default.xhtml</welcome-file>
 </welcome-file-list>

   <!-- JSF mapping -->
 <servlet>
 <servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
 </servlet>

 <!-- Map these files with JSF -->
 <servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
 <servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

context.xml

     <Context>

    <Resource name="jdbc/mkyongdb" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/mkyongdb"/>

   </Context>

Default.xhtml

        <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"   
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  >
<h:head>
    <h:outputStylesheet library="css" name="table-style.css"  />
</h:head>

<h:body>

    <h1>JSF 2.0 + JDBC Example</h1>

    <h:dataTable value="#{customer.getCustomerList()}" var="c"
            styleClass="order-table"
            headerClass="order-table-header"
            rowClasses="order-table-odd-row,order-table-even-row"
        >

        <h:column>
            <f:facet name="header">
                Customer ID
            </f:facet>
                #{c.customerID}
        </h:column>

        <h:column>
            <f:facet name="header">
                Name
            </f:facet>
                #{c.name}
        </h:column>

        <h:column>
            <f:facet name="header">
                Address
            </f:facet>
                #{c.address}
        </h:column>

        <h:column>
            <f:facet name="header">
                Created Date
            </f:facet>
                #{c.created_date}
        </h:column>

    </h:dataTable>

    </h:body>

  </html>

CustomerBean.java

        package com.mkyong;

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

  import javax.annotation.Resource;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.sql.DataSource;

  import com.mkyong.customer.model.Customer;

  @ManagedBean(name="customer")
  @SessionScoped
  public class CustomerBean implements Serializable{

//resource injection
@Resource(name="jdbc/mkyongdb")
private DataSource ds;

//if resource inject is not support, you still can get it manually.
/*public CustomerBean(){
    try {
        Context ctx = new InitialContext();
        ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mkyongdb");
    } catch (NamingException e) {
        e.printStackTrace();
    }

}*/

//connect to DB and get customer list
public List<Customer> getCustomerList() throws SQLException{

    if(ds==null)
        throw new SQLException("Can't get data source");

    //get database connection
    Connection con = ds.getConnection();

    if(con==null)
        throw new SQLException("Can't get database connection");

    PreparedStatement ps 
        = con.prepareStatement(
            "select customer_id, name, address, created_date from customer"); 

    //get customer data from database
    ResultSet result =  ps.executeQuery();

    List<Customer> list = new ArrayList<Customer>();

    while(result.next()){
        Customer cust = new Customer();

        cust.setCustomerID(result.getLong("customer_id"));
        cust.setName(result.getString("name"));
        cust.setAddress(result.getString("address"));
        cust.setCreated_date(result.getDate("created_date"));

        //store all data into a List
        list.add(cust);
    }

    return list;
}
    }

Customer.java

     package com.mkyong.customer.model;

     import java.util.Date;

      public class Customer{

public long customerID;
public String name;
public String address;
public Date created_date;

public long getCustomerID() {
    return customerID;
}
public void setCustomerID(long customerID) {
    this.customerID = customerID;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public Date getCreated_date() {
    return created_date;
}
public void setCreated_date(Date created_date) {
    this.created_date = created_date;
}
   }

pom.xml

  <project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mkyong.common</groupId>
 <artifactId>JavaServerFaces</artifactId>
  <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
  <name>JavaServerFaces Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <repositories>
<repository>
   <id>java.net.m2</id>
   <name>java.net m2 repo</name>
   <url>http://download.java.net/maven/2</url>
 </repository>
 </repositories>

   <dependencies>

<!-- MySQL database driver -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.9</version>
</dependency>

<!-- Spring framework --> 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.6</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>2.5.6</version>
</dependency>



<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.0.0-b13</version>
</dependency>

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.0.0-b13</version>
</dependency>

<!-- EL 2.2 to support method parameter in EL -->
    <dependency>
  <groupId>org.glassfish.web</groupId>
  <artifactId>el-impl</artifactId>
  <version>2.2</version>
</dependency>


    <!-- http://repo1.maven.org/maven2/ -->
     <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>





     </dependencies>

    <build>
   <finalName>JavaServerFaces</finalName>

    <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>2.3.1</version>
       <configuration>
           <source>1.6</source>
           <target>1.6</target>
       </configuration>
   </plugin>
   </plugins>
  </build>
 </project>

If I run this in Tomcat7 I am getting the following error

             SEVERE: Exception sending context initialized event to listener instance                   of class com.sun.faces.config.ConfigureListener
       java.lang.LinkageError: loader constraint violation: when resolving interface method   "javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, com/sun/faces/config/ConfigureListener, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspApplicationContext, have different Class objects for the type javax/el/ExpressionFactory used in the signature
at com.sun.faces.config.ConfigureListener.registerELResolverAndListenerWithJsp(ConfigureListener.java:636)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:226)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
    Jun 15, 2012 2:31:32 PM org.apache.catalina.core.StandardContext startInternal
  SEVERE: Error listenerStart
    Jun 15, 2012 2:31:32 PM org.apache.catalina.core.StandardContext startInternal
 SEVERE: Context [/JavaServerFaces] startup failed due to previous errors
   Jun 15, 2012 2:31:32 PM org.apache.catalina.loader.WebappClassLoader  clearReferencesJdbc
 SEVERE: The web application [/JavaServerFaces] registered the JDBC driver        [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped.       To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
 Jun 15, 2012 2:31:32 PM org.apache.coyote.AbstractProtocolHandler start
 INFO: Starting ProtocolHandler ["http-bio-8080"]
 Jun 15, 2012 2:31:32 PM org.apache.coyote.AbstractProtocolHandler start
  INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jun 15, 2012 2:31:32 PM org.apache.catalina.startup.Catalina start
 INFO: Server startup in 2341 ms

Can Anyone help me out to solve this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:42:39+00:00Added an answer on June 5, 2026 at 11:42 pm

    Ensure that JSF api and implementation jars are available for the container (tomcat) to load. Place the libraries either in WEB-INF/lib are in tomcat lib folder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to connect to a database from my rails app. This database
I'm trying to use JPA to connect my JSF components to database but the
I'm trying to connect to a remote PostgreSql database using powershell. This is my
Im trying to connect to a mysql database on my web server, but I
I'm using HostMonster as my web host and I'm trying connect to a database
I am trying to connect to a remote mysql database from an iPhone. I
I'm trying to connect to a MS SQL Server 2005 Express database that is
I'm trying to connect to a MySQL 5 database using the MySQL ODBC 5.1
I'm trying to connect to a webapi at a location that looks like this
When trying to connect to my database server, i encounter the problem of unknown

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.