Friday, May 17, 2013

JSF2/EJB3 :Preparing Your Environment

JSF is now the preeminent server-side Java web framework, It allows programmers to think about text fields and menus instead of dealing with page flips and request parameters .You really can design web user interfaces by putting components on a form and linking them to Java objects, without having to mix code and markup. A strong point of JSF is its extensible component model, and a large number of third-party components have become available. The flexible design of the framework has allowed it to grow well and accommodate new technologies.In Addition to that JSF2 simplify framework  and  integrate the Java  J2EE technology stack than JSF 1.0. The specification now supports new web technologies such as Ajax and REST.

I decided to blog about the technology for two main reasons:  

 First:I develop many mini-applications in JSF and then forget about them in addition I face problems while working on JSF 2 and I find many solutions and workarounds so I decided to upload my  APP to help many JSF learners and Developers and share my knowledge with them.

 Second: most of JSF Blogs focus on JSF as a user Interface good point but Developers need a full web Application that connect to Database and Implement MVC Framework.So I will try to focus on the user Interface using JSF 2 and also handle the Business logic using EJB3-JPA2 so the basic technologies that we will deal with are:
  • JSF 2 as the User Interface
  • EJB3-JPA2  as the business logic- may be also using hibernate I'm not sure
  • Glassfish 3.1.2 as an application server that supports Java EE 6
  • My Favorite Eclipse Juno For Java EE Developers as Java IDE Tool
  • Oracle Database 11g (but you can use Oracle Database 10g as well) I will use the sample hr schema because it has many tables with their data but of course you can apply examples and source code to any database you like (MySQL,SQL server,etc...)  

  Now How to prepare your Environment to develop JSF2 Application and Deploy them to   GlassFish Server

First: Download and install jdk1.7.01 and set path for java home directory in Environment Variables:

Do exactly as Explained here to download and Install your JDK

Second: Download and Install Eclipse and Configure JDK:

Download Eclipse  from here  and although we will use Eclipse Juno not Indigo but the steps to download and install are the same as mentioned here 

Third: Integrate Glassfish with Eclipse :

Use the following link from here to Download the glassfish 3.1.2 and unzip the archive in your hard Disk  then use the Following Link from here to integrate your server with Eclipse.Note that the only difference is that you will choose glassfish 3.1.2 from the server Type not 3.1.1 as he said in the  link

(for Download and Install Oracle Database use Oracle Documentation to do that as I will not show this because it is out of the scope of the post)

Now let's go to the last step and configure the JNDI DataSource for Oracle Database

Last Step:Configure JNDI DataSource for Oracle DataBase in GlassFish 3.1.2 Server:

in this part we will Create A datasource to be able to connect to Oracle Database.This is a very important step as all of our Applications will use this Datasource on the server to connect to the Database

download Oracle JDBC Driver ojdbc.jar from here and include it in your GlassFish lib folder. The path should look like this: glassfish3\glassfish\lib. 

Start your Server and from the Eclipse window, right-click GlassFish 3.1.2  under Servers tap and click GlassFish -> View Admin Console.

This will open Admin Console in the browser window. in the left-hand side panel under Resources, click on JDBC and then JDBC Connection Pools. In the right hand-side click on new button. 

In New JDBC Connection Pool window, give Oracle_Pool as Pool Name, select javax.sql.DataSource as Resource Type, and Oracle as Database Driver Vendor. On the right upper corner click Next button.

In the log list of additional properties of the next window, find the following properties and fill them with as follows:

ServerName: localhost
URL: jdbc:oracle:thin:@localhost:1521:orcl 
Port: 1521

Note that you can change the previous settings according to your own settings. Click finish and you will see your pool in the list now.Click on ping to test if the pool is created successfully. If not successful, check all the properties again. 

Under the JDBC click on JDBC Resources, click new. In New JDBC Resource window, select Oracle_Pool from Pool Name, and give jdbc/hrConn as JNDI name; don't forget to click OK on the upper right corner.and remember that the JNDI Name You create will be  used in the persistence.xml file to access this Data Source in every application we deploy  

Now after we create and configure our JNDI DataSource in Glassfish we are ready to Develop our Applications in eclipse and deploy them to Glassfish in the next posts

See you soon and Happy JSF Developing