JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







JSF With Database Application with Glassfish

Creating Data Source
  • First Create a Data Source
  • Start the glassfish admin as given below.


Creating Connection Pool

  • Click on JDBC----->Connection Pools as given vbelow.
  • Click on New as shown below.



  • Give any name as here given "mysql1"
  • Select the Resource Type
  • Select the database Vendor as shown below.
  • Click on Next button.


  • click on Add Property
  • Add properties with name value pair as given below.
  • Click on finish button.

  • Now a new connection pool is has been created with the given name "mysql1".
  • Click  on the newly created pool as show below.


  • After clicking the pool the following window is open.
  •   click on ping button to test the connection pool.




  • If ping Succeeded that means yor connection pool is successfully created with no error
    as given below.
  • Now you can use this connection pool.



Creating JDBC  Resource

  • Now click on JDBC --->JDBC Resources
  • click on--> New Button



  • Create A JNDI
  • Give it any name as given below.
  • Select the pool which u have created before.





Creating Table in MYSQL



  • Now Insert some Records into tables.



Creating Application....
  • Click on New---->Dynamic Project


  • Give it any name as given below


  • Create a bean class into myjsf package with name as given below.


EmployeeBean.java
package myjsf;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import javax.sql.DataSource;

public class EmployeeBean {
   private Connection conn;
   
   public void open()
throws SQLException, NamingException {
      if (conn != nullreturn;
      Context ctx = new InitialContext();
      DataSource ds =
(DataSourcectx.lookup("java:comp/env/jdbc/mydb");
      conn = ds.getConnection();   
   }
   
   public Result getAll()
 
throws SQLException, NamingException {
      try {
         open();
         Statement stmt = conn.createStatement();        
         ResultSet result =
stmt.executeQuery
("SELECT * FROM Employee");
         return ResultSupport.toResult(result);
      finally {
         close();
      }
   }
   
   public void close() throws SQLException {
      if (conn == nullreturn;
      conn.close();
      conn = null;
   }  
}

  • Create a jsp file as given below.
index.jsp
  • This index.jsp is used to call the bean file.
  • Here all the data is displayed
<html>
   <%taglib uri="http://java.sun.com/jsf/core"  prefix="f" %>
   <%taglib uri="http://java.sun.com/jsf/html"  prefix="h" %>
   <f:view>
      <head>
        
         <title>
            <h:outputText value="#{msgs.pageTitle}"/>
         </title>
      </head>
      <body>
         <h:form>
            <h:dataTable value="#{employee.all}" var="employee" 

               
               <h:column>
                  <f:facet name="header">
                     <h:outputText value="#{msgs.employeeIdHeader}"/>
                  </f:facet>
                  <h:outputText value="#{employee.Emp_ID}"/>
               </h:column>
               <h:column>
                  <f:facet name="header">
                     <h:outputText value="#{msgs.nameHeader}"/>
                  </f:facet>
                  <h:outputText value="#{employee.Emp_Name}"/>
               </h:column>
               <h:column>
                  <f:facet name="header">
                     <h:outputText value="#{msgs.salary}"/>
                  </f:facet>
                  <h:outputText value="#{employee.sal}"/>
               </h:column>
               
              
            </h:dataTable>
         </h:form>
      </body>
   </f:view>
</html>

  • Create a index.html file
index.html
<html>
   <head>
      <meta http-equiv="Refresh" content= "0; URL=index.faces"/>
      <title>Start Web Application</title>
   </head>
   <body>
      <p>Please wait for the web application to start.</p>
   </body>
</html>


web.xml
  • Configure FacesServlet in web.xml as given below.
  • Configure your data source in <resource-ref> tag
  • In <resource-ref> tag we will put the jndi name create above in jdbc resource creation(glassfish admin console)



<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5">
   <servlet>
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>   
   
   <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.faces</url-pattern>
   </servlet-mapping>  

   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
   </welcome-file-list>

   <resource-ref>
      <res-ref-name>jdbc/mydb</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
   </resource-ref>
</web-app>


faces-config.xml
  • Configure your bean class in faces-config.xml as below.
<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
  version="1.2">
   <application>
      <resource-bundle>
         <base-name>myjsf.messages</base-name>
         <var>msgs</var>
      </resource-bundle>
   </application>

   <managed-bean>
      <managed-bean-name>employee</managed-bean-name>
      <managed-bean-class>myjsf.EmployeeBean</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
   </managed-bean>
</faces-config>

Mysql connector file
  • Copy and paste the MYSQL connector into the lib folder
    of  Your Application as given below
 

Running the project

  • Right Click on the index.jsp
  • Select Run As -->Run On Server
  • Select glassfish application server as given below

  • After runing it will select all the  data from table and display it in the screen.

Output:-



Download  Source Code in Zip


JSF With Database Application  with Tomcat
  • We have to change some code here.
  • First we have to create data source through xml file not on the server.
  • In the context.xml file we have to give the data source details as given below.
  • tomcat_home----->conf------>context.xml
<?xml version='1.0' encoding='utf-8'?>
<Context>
 <Resource name="jdbc/myjsfdb" auth="Container"
type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="mysql"
               
driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/test"/>
</Context>
  • All the Code will be the same as in the above application.
  • Start the tomcat server and deploy the Application. 
Output:-



Download Source