JavaEE6 Weblogic exercice 1

De $1

Version de 09:08, 4 Déc 2024

cette version.

Revenir à liste des archives.

Voir la version actuelle

Introduction

In this exercice we will write a JavaEE application that uses the new "web profile": a single project that can use JSF/JSP/Servlets/Web Services/EJBs/JPA with some limitations (no message driven beans, no timer, etc.) and one advantage: it is easier to manage as all components are located in a single project, and deployment is done in a single jar file. We will use a javaDB/Derby database for this application.

Install the different components

  1. You will need to install weblogic 12c with the Oracle Enterprise Pack for Eclipse (an Eclipse 3.7 for java EE developers that is pre configured with the Weblogic server plugin and some other goodies). Follow the directives of your instructor. Do not forget to create a default domain in your server. When asked for a password, please enter one you will not forget !
  2. Install the JavaDB/Derby database. Follow the instructions from this page (installing a java DB database): Weblogic / Java EE6 FAQ and Guides.
  3. In some other exercices we will use a MySQL database. You may want to install it now also. Again, look at the proper section in:  Weblogic / Java EE6 FAQ and Guides.

Creating a "web profile JavaEE 6 project", write a servlet and an EJB for testing

Add a servlet, run the project

Create a standard dynamic web project using Eclipse, call it for example "Exo1WebProfile". Click twice on the next button and in the last screen, check "Generate web.xml descriptor". Normally we do not need any XML settings in that file (JavaEE 6 replaced the need to XML settings by code annotations) but it will be useful to set the starting URL when we run the project in that file.

Add a Servlet in the project, call it TestServlet, and add it in a package named "servlets". Click finish. Add some lines in the doGet() method, like these:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set the response message's MIME type.
response.setContentType("text/html;charset=UTF-8");
// Allocate a output writer to write the response message into the network socket.
PrintWriter out = response.getWriter();

out.println("<h1>Welcome to Exo1 test servlet!</h1>");
}

 Then modify the web.xml file in order to start the project directly with this servlet. If you look at the Servlet code you will find an annotation that set the URL pattern for the servlet: @WebServlet("/TestServlet"), so let's modifiy the <welcome-file> entry in the web.xml descriptor:

 <welcome-file-list>
<welcome-file>TestServlet</welcome-file>
</welcome-file-list>

Then right click the project, do Run As/Run on server. The first time you will be asked to choose a server. Select "weblogic 12c", check the checkbox so that thos will be a default value for this project and click finish. This should launch weblogic, the console tab should appear, etc. And after a while you should see a browser inside Eclipse showing "Welcome to Exo1 test servlet!".

Add a stateless session bean EJB

Now we will add a stateless session bean EJB to the project. Select as the type of element to add to the project "session bean (EJB 3.x)", add it in a package named "sessions" and name it "HelloWorldBean". Notice that in the wizard you have a select menu for stateless/stateful/singleton and below you may choose to implement some local or remote interfaces. For the moment leave everything with default values (stateless, no interface). Click finish.

Add a method to this EJB. The code should look like this:

package sessions;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
* Session Bean implementation class HelloWorldBean
*/
@Stateless
@LocalBean
public class HelloWorldBean {

/**
* Default constructor.
*/
public HelloWorldBean() {
// TODO Auto-generated constructor stub
}

public String helloWorld() {
return "hello world!";
}
}

Notice the @Stateless and @LocalBean that indicates that we have a stateless session bean that can be only called from the same JVM. Remote session beans are not allowed in a web profile project.

Call the session bean from the servlet

In order to call the bean from the servlet, we need to add a few lines of code. 

Inject the EJB reference into the servlet: add these line below the class declaration:

	@EJB
HelloWorldBean bean;

 Fix the imports then add these lines in the doGet() method:

  out.println("The call to the HelloWorld EJB returned: " + bean.helloWorld());

Then run the project. If everything was ok, you should see a message : "The call to the HelloWorld EJB returned: hello world!".

The @EJB annotation has injected a reference to an instance of the HelloWorld bean. You do not now if a "new" has been performed or if an existing instance has been used or if there are more than one instance running in the web server.

Add JPA to the project, access a javaDB database

Check that Derby/JavaDB is running, connect the sample database to Eclipse and to Weblogic

Normally you should have installed a Derby/JavaDB database to your system using informations from:  Weblogic / Java EE6 FAQ and Guides. Check that you added the database both in Eclipse and in Weblogic.

If you followed the instructions in Eclipse, in the java EE perspective, under the "Data Source Explorer" tab you should see the "sample" javaDB database, and be able to consult the data in the different tables:

javaDBInEclipse.jpg 

And the data:

javaDBData.jpg

Add JPA facet to the project

Eclipse may help you a lot working with JPA but you need to add a JPA facet to the project. Right click on the project then properties/project facets and check the JPA facet. Validate.
 

jpa facet.jpg

Look at your project, a persistence.xml file has been added under META-INF but there is also a new node in your project called "JPAcContent" that makes persistence.xml easier to access.

persistence.xml.jpg

This file is used for configuring the different persistence units that you will use in your project. A persistence unit is an object you will use from your code that works with a given database, using an Object Relational Mapping tool like Hibernate, Eclipse Link, Toplink, etc. This file also is useful for specifying if you are allowed to create tables, alter or delete tables, etc. Leave it for the moment, we will configure it later.

Current persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="Exo1WebProfile" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="eclipselink.target-server" value="WebLogic" />
</properties>
</persistence-unit>
</persistence>

 As you may see it says that we are going to work woth eclipseLink (provider node...), that we are going to work with a local database (transaction-type), etc... these values are not sufficient for what we want to do, we will see later on how to indicate the right value (name of database, etc).

 Add a JPA entity class mapped to the Manufacturer table of the sample database

Let's add an entity class generated from the Manufacturer table of the sample database... add a "jpa entity from table" to the project. If asked to what project you want to add the entity, select the current project. Click next.

Then you are asked to choose the connection and table. Select the connection you added to Eclipse + select APP as a schema. The table will appear, then check MANUFACTURER

Click twice next. Enter "entities" as the package name.

On the last screen you may change the name of the class but it is better to leave the proposed name ("Manufacturer") unchanged.