Accueil > Intranet Michel Buffa > Design Patterns Java EE 6

Design Patterns Java EE 6

De $1


These lab exercices will show you the use of several design patterns with Java EE 6.

Software installation

You will use both Eclipse/Weblogic/MySQL and netbeans 7.2/glassfish/JavaDB for these exercises. Why two different environments ? Because developement with JSF 2 is a lot easier with netbeans and we do not have much time... So, some examples will be run using Eclipse, some other using netbeans...

Install Weblogic + Oracle enterprise pack for eclipse

Install the file named oepe-indigo-installer- get all the default options. We assume that there is a JDK installed on your system.

Once the installation is finished, look at your windows start menu and run "Oracle Enterprise Pack For Eclipse/Weblogic server 12c/Tools/configuration wizard" and create a default domain, using the default options. The instructor will show on the video projector what to set.

When prompted for an admin password, try to enter one you will remember !!!!

Normally, if everything went fine, you should see a new menu in your start menu: "Oracle Enterprise Pack for Eclipse/user projects/base domain"... from this menu you can start the admin server, then when the admin server has been started (in a dos window you should see the message "server started in RUNNING mode"...), you can use the menu to open the admin console. 

If there is an error there, something went wrong, call your instructor...

Install MySQL

There are many version of MySQL installers available. We provide a zip that do not need any install at all. Just unzip the file in c:\mysql for example.

In order to run the MySQL server, open a DOS window, go to c:\mysql\bin\ and run "mysqld.exe", this will start the MySQL daemon. Open another DOS window, go to the same directory and run "mysql.exe -u root", the admin tool. 

Enter "show databases;" to see all the databases available. This should display a table with some prefefined databases.

Install Netbeans

In order to install netbeans 7.2, just run netbeans-7.2.1-ml-javaee-windows.exe" and use all the default options.

The Bookstore example : shows JSP / Servlet model 2 approach (MVC)

This tutorial is available on the web at : but we will just use it with WebLogic instead of with the Resin server.

Step 01 : a model, a facade/DAO, a controller Servlet, a JSP for a table view

You follow the tutorial for the first step : but when in the tutorial they ask to select Resin as a new runtime environment, just select Weblogic in the list. The rest of the tutorial is ok... Or you can just open the Eclipse project provided named "BookStoreStep01", that corresponds to this part of the tutorial.

If you follow the tutorial, you will get a project like this:


With :

  • A Model in the com.bookstore package,
  • A facade / DAO with its interface and its implementation in,
  • A Servlet that acts as HTTP controller in the file located in the com.bookstore.web package,
  • A JSP that acts as a view, in the WebContent/WEB-INF/pages/book-list.jsp page. Thus view references the model using the EL langage.

In order to run and deploy this project, use the Java EE perspective, go to the "server tab", right click on "oracle weblogic..." and chose "add and remove". Then select your project BookStoreStep01 and Ok. This will deploy your project on weblogic. Then use "run on server" on your project. This should display a list of books in the JSP page.

Look at how the code is organized, how we go from the Servlet to the JSP etc. This examples uses CDI for injecting the Facade into the Servlet using CDI, like explained in the course.

Step 02 : adding the CRUD operations...

This is the tutorial part 2:

It is also available in the BookStoreStep02 Eclipse project.

In this step, a new Servlet has been added for handling update/insert new book operations, as well as a new JSP that will display a form that will be use for both editing an existing book or creating a new book. The business part is already implemented in the facade, we are just dealing with HTTP control and view here. The link with the table view is just a <a href> added in the table.

Step 03: just adding come CSS, headers and footers...

This step is not very relevant to our design pattern course, you may check it at :

it corresponds to the BookStoreStep03 Eclipse project.

Step 04: Add a second implementation for the facade, that uses JDBC, use injection with qualifiers

This example corresponds to this tutorial:

BEWARE : all the MySQL, database creation part must NOT be followed in this tutorial, we will proceed a bit differently.

This step corresponds to the BookStoreStep04 Eclipse project.

Create a Bookstore MySQL database, add a table named "book" inside

In order to run this project, you will need to create a database named "Bookstore" in the MySQL server you installed today. Do not follow what is indicated in the online tutorial, instead follow these steps:

  1. Go to the c:\mysql\bin dir in a DOS window,
  2. Execute "mysql -u root",
  3. Execute "create database Bookstore;"
  4. Execute "use Bookstore;"
  5. Execute "CREATE TABLE book (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,  title VARCHAR(80), description VARCHAR(2000), pubdate DATE, price DECIMAL);"
  6. Verify that your table has been created: "show tables;", this should display a table named "book"...

 Then in Eclipse, you can add the database we just created to the "Datasource Explorer" tab of the Java EE perspective.

Add the connection to the DB admin tool in Eclipse java EE perspective

  1. In Eclipse, in the Java EE perspective, locate a tab called "Data Source Explorer", right click on "Database Connections" and choose "New" in order to add a new connection.
  2. In the next dialog, choose MySQL as the database type,
  3. Then you must indicate which JDBC driver you are going to use. Choose "MySQL Driver" in the drop down list, but this is not enough, you need to set the proper jar file to use. So click on the small rounded button at the right '"new driver definition", then on the "jar list" tab. Remove proposed jar and click on the "add jar/zip" button on the right.
  4. Go to c:\mysql"  and select the mysql-connector-java-5.1.14-bin.jar file. This is the JDBC driver for our version of MySQL.
  5. Enter the database info as in the next screenshot,  click on the "test connection" button on the right.

 mysql driver.jpg

 Add this new Database as a DataSource in Weblogic

We need to add this database in Weblogic + give it a JNDI name so that we can access it from our program. The JNDI name used in the tutorial is "jdbc/bookstore", so we will use that in WebLogic.


In WebLogic 12c, run the admin server from the windows start menu, then open the weblogic admin console (also from start menu). 

  1. Go to the "source de données"/datasources link in the main page. Click on it

  2. Use the nouveau/new button in order to add a datasource,
  3. Select "source de données generiques"/generic datasource in the drop down menu
  4. Next dialog : enter a name for the database : I suggest "Bookstore", enter a JNDI name for accessing the database from a java EE application. Use "jdbc/bookstore".  Choose "MySQL" as the database type. Click next.
  5. Next dialog click next in order to validate the JDBC driver proposed by default (WebLogic comes with a MySQL driver pre-installed)
  6. Next dialog: click next (transactions options),
  7. Next dialog: enter the same infos as when you connected the database to Eclipse (host = localhost, name of database = Bookstore). Click Next,  
  8. Next dialog, click "Test the connection", if everything is ok, click next,
  9. Next dialog, check the AdminServer as a target for this database. Do not forget to select the target server otherwise your datasource will not be exposed to external applications.
  10. Click finish.

Yepo ! We finished with the Database setting part of this exercise, let's do more interesting things now !

Either do the tutorial, starting at the Create Repository class that Implements BookRepository using JDBC section, or look at the BookStoreStep04 eclipse project.

Notice that in that project, we are going to provide a second implementation of the façade that uses JDBC and the Bookstore database we created. We will use injection for getting a reference to the facade object and we will use CDI qualifiers (that are user friendly labels) in order to inject the implementation we prefer. 

A Simple JSF2 login/password example

For this example, we will use netbeans and glassfish. So please, shut down Eclipse, Weblogic and MySQL. Run netbeans.

We will just look at a very simple project that uses JSF2 for handling a login/password session.

This example is interesting as it shows :

  • Collaboration between a JSF bean that holds models in the form of properties (attribute + getters and setters), that holds "controller" methods (called "action methods"), and  a simple JSF page that holds graphical views (in this case a HTML form with two input fields).
  • How scopes are handled using code annotations.

Open with netbeans the project named :, unzip it somewhere (Documents\Netbeans projects is the standard directory...), and run it. It's the exact example illustrated during the course. We will comment it live.

A more complicated example that uses JSF 2 / EJBs facade / EJB singleton / JPA


Mots clés:
Images (2)
Voir 1 - 2 sur 2 images | Voir tout
Aucune description
mysql dri...  Actions
Aucune description
Book1.jpg  Actions
Commentaires (0)
Vous devez être connecté pour poster un commentaire.