Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "WTP JEE5 Test Scenarios"

(UC-1(d) EJB3 WEB CLIENT USE CASE)
(Correction in the code snippet for the HelloWorldMessage class)
 
(6 intermediate revisions by one other user not shown)
Line 11: Line 11:
  
 
== Prereqs ==
 
== Prereqs ==
To try these use cases you will need the latest build of WTP, [http://tomcat.apache.org Apache Tomcat] version 6, [http://java.sun.com/javase/downloads/index.jsp JDK 1.5 and up], [http://java.sun.com/javaee/javaserverfaces/download.html JSF v1.1 Reference Implementation] or [http://myfaces.apache.org Apache MyFaces1.1.5], [http://www.oracle.com/technology/products/ias/toplink/jpa/download.html TOPLink Essentials], and [http://derby.apache.org Apache Derby].  Follow their instructions to install these software. Additionally, EJB/Ear use cases require [http://javashoplm.sun.com/ECom/docs/Welcome.jsp?StoreId=22&PartDetailId=sjsas_pe-9.0_01-ml-oth-JPR&SiteId=JSC&TransactionId=noreg Glassfish] or a similar JEE 5 compatible application server and a WTP server adapter that supports JEE5 features. There is an [http://wiki.eclipse.org/images/3/37/Org.eclipsecon.wtp.serveradapters.zip example server adapter] I and John Lanuti have created during the eclipse con tutorial, which can be used with Glassfish.
+
To try these use cases you will need the latest build of WTP, [http://tomcat.apache.org Apache Tomcat] version 6, [http://java.sun.com/javase/downloads/index.jsp JDK 1.5 and up], [http://java.sun.com/javaee/javaserverfaces/download.html JSF v1.1 Reference Implementation] or [http://myfaces.apache.org Apache MyFaces1.1.5], [http://www.oracle.com/technology/products/ias/toplink/jpa/download.html TOPLink Essentials], and [http://derby.apache.org Apache Derby].  Follow their instructions to install these software. Additionally, EJB/Ear use cases require [http://geronimo.apache.org Apache Geronimo], [http://www.jboss.org JBoss v5.0],  [http://javashoplm.sun.com/ECom/docs/Welcome.jsp?StoreId=22&PartDetailId=sjsas_pe-9.0_01-ml-oth-JPR&SiteId=JSC&TransactionId=noreg Glassfish] or a similar JEE 5 compatible application server. WTP comes with a server adapter for JBoss v5.0, you can obtain the latest Geronimo adapters for WTP 2.0 from [http://geronimo.apache.org/development-tools.html Geronimo Server Adapters], for other servers you will need a WTP server adapter that supports JEE5 features. There is an [http://wiki.eclipse.org/images/3/37/Org.eclipsecon.wtp.serveradapters.zip example glassfish server adapter] that me and John Lanuti have created during an eclipsecon 2007 tutorial.
  
 
== UC-1(a) SIMPLE WEB APPLICATION USE CASE ==
 
== UC-1(a) SIMPLE WEB APPLICATION USE CASE ==
Line 145: Line 145:
 
             this.message = message;
 
             this.message = message;
 
         }
 
         }
         public Long getId()...
+
         public Long getId() {
         public void setId(Long id)...
+
            return id;
         public String getLanguage()...
+
        }
         public void setLanguage(String language)...
+
         public void setId(Long id) {
         public String getMessage()...
+
            this.id = id;
         public void setMessage(String message)...
+
        }
 +
         public String getLanguage() {
 +
            return language;
 +
        }
 +
         public void setLanguage(String language) {
 +
            this.language = language;
 +
        }
 +
         public String getMessage() {
 +
            return message;
 +
        }
 +
         public void setMessage(String message) {
 +
            this.message = message;
 +
        }
 
         @Override
 
         @Override
 
         public String toString() {
 
         public String toString() {
                return message;
+
            return message;
 
         }
 
         }
 
     }
 
     }
Line 190: Line 202:
  
 
*  Step 9 - In the next use case we will extend the simple Web application to be a client to the EJB.
 
*  Step 9 - In the next use case we will extend the simple Web application to be a client to the EJB.
 
  
 
== UC-1(d) EJB3 WEB CLIENT USE CASE ==
 
== UC-1(d) EJB3 WEB CLIENT USE CASE ==
  
This use case is a simple Web client to the previos EJB3 application scenario that involves creating a dependency to the EJB3 module, targeting it to a JEE5 runtime, and testing it.
+
This use case is a simple Web client to the previous EJB3 application scenario that involves creating a dependency to the EJB3 module, targeting it to a JEE5 runtime, and testing it.
 
    
 
    
 
     JBoss v5.0 BUG: If you are using JBoss v5.0 fo testing this use case.  There is a JBoss v5.0 bug that prevents WTP  
 
     JBoss v5.0 BUG: If you are using JBoss v5.0 fo testing this use case.  There is a JBoss v5.0 bug that prevents WTP  
Line 234: Line 245:
 
   [[Image:UC1d-StartingServer.png]]
 
   [[Image:UC1d-StartingServer.png]]
  
== UC-2 Web Application with using JSF 1.1 and JPA 1.0 ==
 
  
This use case is and adaptation of the [http://www.eclipse.org/dali/docs/tutorial/jsf/dali_jsf_tutorial.htm Dali Tutorial]. This use case is about building and deploying dynamic web application for an order-entry system using JPA 1.0 and JSF 1.1. The complete application is packaged as a web application.
 
  
 +
== UC-1(e) EAR5 Combining EJB3 /WEB CLIENT USE CASE ==
  
 +
This use case is a simple Enterprise Application v5.0 that combines the previous EJB3 application scenario and the Web Client in an EAR file, targeting it to a JEE5 runtime, and testing it.
 +
 
 +
 +
 +
* Step 1 - Go to Installed Runtimes and add JBoss v5.0 as a runtime.
 +
    EJB 3.0, and all other WTP JEE5 support should work with any server runtime
 +
    that has JEE 5  capability such as JBoss v5, IBM WAS, BEA WebLogic Server,
 +
    Oracle AS, JOnAS, Apache Geronimo among many others, although there maybe
 +
    some delays in providing server adapters for all.
 +
 +
    [[Image:UC1c-CreateNewRuntime-1.png]]
 +
    [[Image:UC1c-CreateNewRuntime-2.png]]
 +
 +
* Step 2 - Create a new Enterprise Application Project named HelloEar, select JBoss v5.0 as the target runtime
 +
  [[Image:UC1d-NewEARProject.png]]
 +
 +
* Step 3 - Select EAR 5.0 click next to accept defaults.
 +
  [[Image:UC1d-NewEarFacets.png]]
 +
 +
* Step 4 - Click Next. In the next wizard page, you have the option to specify the mdoules in the EAR module.  Choose HelloWorldEJB and HelloWorldWeb.  If you have started this usecase from scratch, you can create new Web and EJB3 modules and follow the steps described in th eprevious usecases to develop them. Do NOT check the option to create a Deployment Descriptor, EAR 5.0 modules can figure out what is in an EAR module automatically. Click Finish.
 +
[[Image:UC1d-GenerateDeploymentDescriptor.png]]
 +
 +
 +
*  Step 5 - The global JNDI name for the EJB3 changes when an EJB is packaged in an EAR file.  Each application server may have its own way of naming the EJBs. Our JSP file in the Web client needs to be edited. For JBoss v5.0, make sure that the code looks like the following:
 +
 +
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 +
        pageEncoding="ISO-8859-1"%>
 +
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     
 +
        "http://www.w3.org/TR/html4/loose.dtd">
 +
    <%@page import="javax.naming.InitialContext"%>
 +
    <%@page import="demo.HelloWorld"%>
 +
    <html>
 +
    <head>
 +
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 +
    <title>Hello World</title>
 +
    </head>
 +
    <body>
 +
    <%
 +
        InitialContext ctx = new InitialContext();
 +
        HelloWorld message =  (HelloWorld)ctx.lookup("HelloEar/HelloWorldStateless/remote");
 +
    %>
 +
    <%= message.hello("en")%>
 +
    </body>
 +
    </html>
 +
 +
*  Step 6 - Choose the test.jsp, and choose Run On>Run On Server, WTP will deploy the Ear module to the server, start it, open the JSP file and you sill see the Hello World Message.
 +
 +
[[Image:UC1e-DeployEar.png]]
 +
 +
 +
    JBoss v5.0 BUG: If you are using JBoss v5.0 fo testing this use case.  There is a JBoss v5.0 bug that prevents WTP
 +
    from redeploying an EJB jar, ear or a war file to JBoss (see http://jira.jboss.com/jira/browse/JBAS-4310).  As a
 +
    workaround stop the JBoss v5.0 server before you start this use case.
 +
 +
 +
== EXTENDED USE CASES (JPA 1.0 + JSF 1.2) ==
 +
 +
These scenarios extend the use cases described in the first section by adding JPA 1.0 facet to the EJB3 module to persist HelloWorldMessage and using JSF 1.2 for the Web client.  They build on the last scenario that combines the EJB3 module and the Web application using an EAR file.
 +
 +
 +
JPA 1.0 use case is and adaptation of the [http://www.eclipse.org/dali/docs/tutorial/jsf/dali_jsf_tutorial.htm Dali Tutorial]. This use case is about building and deploying dynamic web application for an order-entry system using JPA 1.0 and JSF 1.1. The complete application is packaged as a web application.
 +
 +
EJB3 with JPA use case involves creating an EJB 3.0 stateless session bean, that uses JPA 1.0 to map a simple class.  A web application using JSF is the EJB client.  Whole application is packaged as an EAR.
  
== EJB/EAR SCENARIOS ==
+
=== UC-2(a) EAR5 Combining JPA version of EJB3 /WEB CLIENT USE CASE ===
The second set of scenarios are for EJB 3.0 and EAR 5.  Since EJBs are meaningless without a client, this use case involves creating an EJB 3.0 stateless session bean, that uses JPA 1.0 to map a simple class.  A web application using JSF is the EJB client.  Whole application is packaged as an EAR.
+
  
=== UC-3 SIMPLE SESSION EJB 3.0/JPA 1.0 ===
 
 
This use case is the most creates a simple POJO mapped using JPA 1.0.  A stateless session references the entity manager to query the DB.  The EJB jar is packaged in an EAR module
 
This use case is the most creates a simple POJO mapped using JPA 1.0.  A stateless session references the entity manager to query the DB.  The EJB jar is packaged in an EAR module
 
* Step 1 - Go to Installed Runtimes and add Glassfish as a runtime.
 
* Step 1 - Go to Installed Runtimes and add Glassfish as a runtime.
Line 251: Line 322:
 
     some delays in providing server adapters for all.
 
     some delays in providing server adapters for all.
  
* Step 2 -  Create a new EJB 3.0 Project, with JPA 1.0 facet named HelloWorldEJB, select Glassfish as the target runtime.
+
* Step 2 -  If you have not done so, create an EJB3 module, a Web module and include them in an EAR module as described in the previous use cases. Follow the step to develop the testçjsp and the HelloWorld EJB.  
* Step 3 -  Make sure that  HelloWorldEJB is added to HelloWorldEAR.
+
 
* Step 4 -  Use the data tools to create a connection to the new Derby database named myderbydb. '''(Note: Can the data tools steps be clarified for those unfamiliar w/ Dali and Derby?  Or can we provide a link to a pre-configured Derby DB?)'''
+
* Step 3 -  Use the data tools to create a connection to the new Derby database named myderbydb. Visit [[How to create a connection to Derby using DTP]] if you need assistance to create a DTP connection.
* Step 5 -  Create a connection to it and create a new table using the following script:
+
 
     CREATE TABLE HELLOWORLD (
+
* Step 4 -  Create a connection to it and create a new table using the following script:
      `ID` varchar(10) NOT NULL,
+
     CREATE TABLE HelloWorldMessage(
      `MESSAGE` varchar(50) NOT NULL,
+
        ID varchar(10) NOT NULL,
      `LANGUAGE` varchar(50) NOT NULL,
+
        MESSAGE varchar(50) NOT NULL,
      PRIMARY KEY (`ID`)
+
        LANGUAGE varchar(50) NOT NULL,
    );
+
        PRIMARY KEY(ID)
* Step 6 -  Add a few rows of data such as
+
        );
 +
 
 +
* Step 5 -  Add a few rows of data such as
 
     1 Hello World! en
 
     1 Hello World! en
 
     2 Merhaba Dünya tr
 
     2 Merhaba Dünya tr
* Step 7 -  Create a package called demo and add the following class which is JPA mapped
 
  
    package demo;
+
* Step 6 -  Choose project properties to add a new Facet.  Choose Java Persistence version 1.0.  Click Next.
    import java.io.Serializable;
+
[[Image:UC2a-JPAFacet.png]]
 +
 
 +
* Step 6 -  In the modify facet page, choose the generic platform and the database connection you have created. You will have two choices for JPA implementation, the default one that comes with the server runtime (JBoss v5.0), or choose a specific implementation library. You can use the links below the selection box to define new ones if you have not done so already.  Choose  Click Next.
 +
[[Image:UC2a-ModifyFacet-step1.png]]
 +
 
 +
* Step 6-Optional-  Click on Configure user libraries to define TOPLink Essentials as JPA implementation. Add TOPLink jar and click OK.  When you are done choose it as the default JPA library.
 +
[[Image:UC2a-ModifyFacet-step2.png]]
 +
 
 +
* Step 7 -  Choose Window>Open Perspective and switch to the JPA Development perspective.
 +
[[Image:UC2a-SwitchJPAPerspective.png]]
 +
 
 +
* Step 8 -  Edit the HelloWorldMessage class using the Java editor in the JPA Development perspective.
 +
[[Image:UC2a-JPAPerspective.png]]
 +
 
 +
* Step 9 -  In the Java editor click on the HelloWorldMessage class text.  In the JPA Details view, select Map As: Entity, table name and schema to reflect our choices for this class.
 +
[[Image:UC2a-ClassJPADetail.png]]
 +
 
 +
* Step 10 -  In the JPA outline click on the attribute id.  In the JPA Details view, select Map As: Id.
 +
[[Image:UC2a-IDJPADetail.png]]
 +
 
 +
* Step 11 -  When you are done HelloWorldMessage class will look like the following:
 +
 
 +
 
 
     @Entity
 
     @Entity
     public class HelloWorld implements Serializable{
+
    @Table(schema="DALI", name = "HELLOWORLDMESSAGE")
         private static final long serialVersionUID = 1L;
+
     public class HelloWorldMessage implements Serializable {
 +
         private static final long serialVersionUID = 1L;
 
         @Id
 
         @Id
         private String id;
+
         private Long id;
 +
        private String language;
 
         private String message;
 
         private String message;
        private String language;
 
        public HelloWorld() {
 
        }
 
        public String getId() {
 
 
           ...
 
           ...
'''(Note: We had to add an @Table(schema="APP", name="HELLOWORLD") in order to get this working.)'''
 
* Step 8 -  Open your persistence.xml (create one if it does not exists in….) file, and make sure that it looks like:
 
        <persistence version="1.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_1_0.xsd">
 
        <persistence-unit name="HelloWorldPU">
 
          <jta-data-source>jdbc/MyDataSource</jta-data-source>
 
          <class>demo.HelloWorld</class>
 
        </persistence-unit>
 
        </persistence>
 
  
 +
'''(Note: We had to add an @Table(schema="DALI", name="HELLOWORLDMESSAGE") in order to get this working.)'''
 +
 +
* Step 8 -  Open your persistence.xml in the META-INF folder. (create one if it does not exists in….) file, and make sure that it looks like:
 +
    <?xml version="1.0" encoding="UTF-8"?>
 +
    <persistence version="1.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_1_0.xsd">
 +
        <persistence-unit name="HelloWorldPU">
 +
            <jta-data-source>jdbc/MyDataSource</jta-data-source>
 +
            <mapping-file>META-INF/orm.xml</mapping-file>
 +
        </persistence-unit>
 +
    </persistence>
  
 
* Step 9 -  Refer to Application Server manuals to create a new DataSource to the Derby database with the JNDI name “jdbc/MyDataSource”.
 
* Step 9 -  Refer to Application Server manuals to create a new DataSource to the Derby database with the JNDI name “jdbc/MyDataSource”.
  
'''(Note:  We had to bring up the Admin Console for Glassfish by pointing to http://localhost:4848 and entering userid: admin password: adminadmin)'''
+
'''(Note:  In JBoss you create an XML file in the server/default/deploy folder. In Glassfish, bring up the Admin Console for Glassfish by pointing to http://localhost:4848 and entering userid: admin password: adminadmin)'''
 +
 
 +
* Step 10 -  Update the code for your EJB3 stateless session bean implementation  HelloWorldBean, and make sure that it looks like the following:
 +
 
 +
    @Stateless(name = "HelloWorldStateless", mappedName = "HelloWorldStateless")
 +
    @Remote(HelloWorld.class)
 +
    public class HelloWorldBean implements HelloWorld {
 +
        @PersistenceUnit(name = "HelloWorldPU")
 +
        EntityManagerFactory emf;
 +
        public HelloWorldMessage hello(String language) {
 +
            Query query = emf.createEntityManager().createQuery(
 +
                    "select h from HelloWorld h where h.language = :lang ");
 +
            query.setParameter("lang", language);
 +
            List<HelloWorldMessage> list = query.getResultList();
 +
            if (list.size() == 0)
 +
                return list.get(0);
 +
            return null;
 +
        }
 +
    }
  
* Step 10 Create a package called demo.session and create a class named HelloWorldSession, and make sure that it looks like the following:
+
* Step 11 You are done with creating an EJB 3.0 Module.  Next you will create a client Web application.
  
        @Stateless(name="HelloWorldStateless")
+
* Step 12 -  There is no need to change the Web module now. Select test.jsp, Run As > Run On Server,  the ear should be packaged and deployed with EJB and WAR, page should display the message.
        public class HelloWorldSession
+
          implements HelloBeanFacade
+
        {
+
          @PersistenceUnit(name = "HelloWorldPU")
+
          EntityManagerFactory emf;
+
          public HelloWorldSession() {
+
          }
+
          public HelloWorld hello(String language) {
+
            Query query = emf.createEntityManager().
+
              createQuery("select h from HelloWorld h where h.language = :lang ");
+
            query.setParameter("lang",language);
+
            List<HelloWorld> list = query.getResultList();
+
            if (list.size() == 0)
+
              return list.get(0);
+
            return null;
+
          }
+
        }
+
  
* Step 11 -  Add the interfaces as usual:
 
  
          public interface HelloBeanFacade {
+
=== UC-2(bEAR5 Combining JPA version of EJB3 / JSF version WEB CLIENT USE CASE ===
            public HelloWorld hello(String language);
+
          }
+
* Step 12 -  You are done with creating an EJB 3.0 Module. Next you will create a client Web application.
+
  
=== JSF WEB Client for EJB ===
+
'''(Note:  THIS SCENARIO HAS NOT BEEN UPDATED YET BECAUSE WE FAIL TO EXECUTE IT WITH THE LATEST DRIVER)'''
  
 
* Step 12 -  Create a new Dynamic Web Project (Web 2.5, Java 5, JSF 1.1) named HelloWorldWeb, select GlassFish as the target runtime.  Make sure that HelloWorldWeb is also added to the HelloWorldEAR.
 
* Step 12 -  Create a new Dynamic Web Project (Web 2.5, Java 5, JSF 1.1) named HelloWorldWeb, select GlassFish as the target runtime.  Make sure that HelloWorldWeb is also added to the HelloWorldEAR.

Latest revision as of 12:14, 23 May 2007

WTP 2.0 JEE 5 Use Cases

These scenarios are created to test the basic functionality that we would expect from WTP 2.0 to support JEE5. Please note that NO wizards are used other than the ones needed to create the servers, projects and add the necessary facets to these projects. The idea is if we are provided with valid JEE5 module and components (such as EJBs, JPA, Servlets, JSPs, etc), WTP 2.0 should support it. This includes Web, EJB, and EAR and Run As> Run on Server support. Of course this involves basic models for deployment descriptors (sans Annotation injections). Annotation support will only include that provided by JDT.

If you have questions, comments, etc., about these use cases, send a note to the WTP Dev List or contact me, Naci Dai.

Test Plan

As we get close to releasing 2.0, we will report on the status of these use-cases with each I-Build. After an I-Build is declared (Typically on Thursdays), we will run the following use-cases using this build and report any problems via bugzilla. To facilitate easy reporting and searching of these bugs, we will use an indexing key called JEE5-UC in Status Whiteboard. You can use a search like JEE5-UC to list them. Use cases and steps are numbered to make it easy to refer to them.

Please try these use cases or similar ones yourselves and help us improve the quality of WTP tools for 2.0 release.

Prereqs

To try these use cases you will need the latest build of WTP, Apache Tomcat version 6, JDK 1.5 and up, JSF v1.1 Reference Implementation or Apache MyFaces1.1.5, TOPLink Essentials, and Apache Derby. Follow their instructions to install these software. Additionally, EJB/Ear use cases require Apache Geronimo, JBoss v5.0, Glassfish or a similar JEE 5 compatible application server. WTP comes with a server adapter for JBoss v5.0, you can obtain the latest Geronimo adapters for WTP 2.0 from Geronimo Server Adapters, for other servers you will need a WTP server adapter that supports JEE5 features. There is an example glassfish server adapter that me and John Lanuti have created during an eclipsecon 2007 tutorial.

UC-1(a) SIMPLE WEB APPLICATION USE CASE

This use case is the most basic Web application scenario that involves creating the Web module, targeting it to a JEE5 runtime, and testing it using Run As> Run on Server

  • Step 1 - Go to Installed Runtimes and add Tomcat 6 as a runtime.
    EJB 3.0, and all other WTP JEE5 support should work with any server runtime
    that has JEE 5  capability such as JBoss, IBM WAS, BEA WebLogic Server, 
    Oracle AS, JOnAS, Apache Geronimo among many others, although there maybe 
    some delays in providing server adapters for all.

  • Step 2 - Create a new Dynamic Web Project named HelloWorldWeb, select Tomcat 6 as the target runtime
  WtpJEE5UC1DynamicWebProj.gif
  • Step 3 - Select Dynamic Web Module 2.5, and Java 5 as facets, click next to accept defaults. A new Web 2.5 project is created.
  WTPJEE5UC1Facets.gif
  • Step 4 - Click Next. In the next wizard page, you have the option to specify context root and content directories, and option to create a Deployment Descriptor.
UC1-GenerateDeploymentDescriptor.png

Make sure that Generate Deployment Descriptor is checked. WTP will create the web.xml file WebContents/Web-Inf folder. web.xml should look like the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5"
     metadata-complete="true">
    </web-app>
  • Step 5 - Add a new JSP file called test.jsp with the following content:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       
      "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
       Hello World!
    </body>
    </html>
  • Step 6 - Right click test.jsp, Choose Run As: Run on Server, select Tomcat 6 as the server
  • Step 7 - Tomcat 6 starts and results are correctly displayed (using current code in CVS, 3/28)

UC-1(b) ADD A SERVLET TO SIMPLE WEB APPLICATION USE CASE

This use case adds a servlet to the simple Web application scenario (previos use-case)and testing it using Run As> Run on Server

  • Step 1 - Use File->new to choose the Servlet Wizard. Complete the wizard pages to create a Servlet class in a package named demo with the class named HelloWorldServlet. The servlet should be the subclass of HttpServlet. Click next to proceed to the next pages and give the name helloworld to the servlet, /helloworld as the URL mapping and select check the box to create the doGet method.

UC1b-CreateNewServlet.png

  • Step 2 - The wizard will create the Servlet class and add the required entries to the web.xml file.
   <?xml version="1.0" encoding="UTF-8"?>
   <web-app id="WebApp_ID" version="2.5"
       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">
       <servlet>
           <servlet-name>helloworld</servlet-name>
           <servlet-class>demo.HelloWorldServlet</servlet-class>
       </servlet>
       <servlet-mapping>
           <servlet-name>helloworld</servlet-name>
           <url-pattern>/helloworld</url-pattern>
       </servlet-mapping>
   </web-app>
  • Step 2 - Edit the Servlet class so that the code looks like the following:
   public class HelloWorldServlet extends HttpServlet {
       @Override
       public void doGet(HttpServletRequest req, HttpServletResponse resp)
           throws ServletException, IOException {
           resp.getWriter().print("Hello World");	
       }
   }
  • Step 4 - Tomcat 6 starts and results are correctly displayed

UC-1(c) SIMPLE EJB3 APPLICATION USE CASE

This use case is the most basic EJB3 application scenario that involves creating the EJB3 module, targeting it to a JEE5 runtime, and testing it.

  • Step 1 - Go to Installed Runtimes and add JBoss v5.0 as a runtime.
    EJB 3.0, and all other WTP JEE5 support should work with any server runtime
    that has JEE 5  capability such as JBoss v5, IBM WAS, BEA WebLogic Server, 
    Oracle AS, JOnAS, Apache Geronimo among many others, although there maybe 
    some delays in providing server adapters for all.
   UC1c-CreateNewRuntime-1.png
   UC1c-CreateNewRuntime-2.png
  • Step 2 - Create a new EJB Project named HelloWorldEJB, select JBoss v5.0 as the target runtime
  UC1c-NewEJB3Project.png
  • Step 3 - Select EJB Module 3.0, and Java 5 as facets, click next to accept defaults.
  UC1c-NewEJB3Facets.gif
  • Step 4 - Click Next. In the next wizard page, you have the option to specify source directories, and option to create a Deployment Descriptor.
UC1c-GenerateDeploymentDescriptor.png

Make sure that Generate Deployment Descriptor is unchecked. WTP will create the new EJB Project.

UC1c-NewEJBProject.png
  • Step 5 - Now we can add a new Stateless EJB. First start with the Interface. Create a new interfaces using the New Interface wizard. Place it in the demo package and name it HelloWorld.

UC1c-NewInterface.png

Edit the interface and make sure that the code looks like the following. When you save you will have an error because we did not create the HelloWorldMessage class yet.

   public interface HelloWorld {
       public HelloWorldMessage hello(String language);
   }

Next we will create the HelloWorldMessage class, which can say HelloWorld in many different languages!

  • Step 5 - Create a new class using the New Class wizard. Place it in the demo package and name it HelloWorldMessage. Edit the class and make sure that the code looks like the following. It will have three attributes named id, language and message, getter and setter methods, a toString method that prints the message and constructors. When you save your previos error messages should dissappear.
   public class HelloWorldMessage implements Serializable {
       private static final long serialVersionUID = 1L;
       //attributes
       private Long id;
       private String language;
       private String message;
       public HelloWorldMessage() {
           super();
       }
       public HelloWorldMessage(Long id, String language, String message) {
           super();
           this.id = id;
           this.language = language;
           this.message = message;
       }
       public Long getId() {
           return id;
       }
       public void setId(Long id) {
           this.id = id;
       }
       public String getLanguage() {
           return language;
       }
       public void setLanguage(String language) {
           this.language = language;
       }
       public String getMessage() {
           return message;
       }
       public void setMessage(String message) {
           this.message = message;
       }
       @Override
       public String toString() {	
           return message;
       }
   }
  • Step 6 - It is time to create the Stateless EJB that implements the HelloWorld interface. Create a new class using the New Class wizard. Place it in the demo package and name it HelloWorldBean.

UC1c-NewStatelessBean.png

Edit the class and make sure that the code looks like the following. It will have two annotations @Stateless and @Remote and implements the hello message defined in the interface.

   @Stateless(name="HelloWorldStateless")
   @Remote(HelloWorld.class)
   public class HelloWorldBean implements HelloWorld {
       public HelloWorldMessage hello(String language) {
           // Return a hard coded message for now
           return new HelloWorldMessage(0L,"en","Hello World!");
       }
   }

That is it for the EJB3 development. You can now create a new Server for the JBoss and deploy our EJB.

  • Step 6 - Choose the HelloWorldEJB and choose Run On>Run On Server, and create new new server for JBoss v5.0.

UC1c-NewServer.png

  • Step 7 - Add the EJB3 project to the new server.

UC1c-AddProjectToServer.png

  • Step 8 - Click finish. WTP will deploy the EJB jar to JBoss and start it. When JBoss start you should see a message like the following that confirms that EJB has been deployed to the server succesfully.
   17:50:46,875 INFO  [Catalina] Server startup in 203 ms
   17:50:59,671 INFO  [MCKernelAbstraction] installing bean: jboss.j2ee:jar=HelloWorldEJB.jar,name=HelloWorldStateless,service=EJB3 with dependencies:
   17:51:02,890 INFO  [EJBContainer] STARTED EJB: demo.HelloWorldBean ejbName: HelloWorldStateless


  • Step 9 - In the next use case we will extend the simple Web application to be a client to the EJB.

UC-1(d) EJB3 WEB CLIENT USE CASE

This use case is a simple Web client to the previous EJB3 application scenario that involves creating a dependency to the EJB3 module, targeting it to a JEE5 runtime, and testing it.

   JBoss v5.0 BUG: If you are using JBoss v5.0 fo testing this use case.  There is a JBoss v5.0 bug that prevents WTP 
   from redeploying an EJB jar, ear or a war file to JBoss (see http://jira.jboss.com/jira/browse/JBAS-4310).  As a 
   workaround stop the JBoss v5.0 server before you start this use case.
  • Step 1 - Go to HelloWorldWeb project and open its properties window. Click on the J2EE dependencies item on the list to choose HelloWorldEJB as a module that our Web application will depend on. By doing this we can access the types defined in the EJB module from the Web application and make remote calls to the stateless EJB.
   UC1d-ModuleDependency.png
  • Step 2 - Edit the test.jsp JSP to make a call to our EJB. JNDI naming for remote and local EJB interfaces is not prtable among application servers so you will have to identify how it is implemented. For JBoss the suffix /remote is appended to the end of the JNDI name for the EJB. So we will use HelloWorldStateless/remote. Make sure your JSP looks like the following:
   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       
       "http://www.w3.org/TR/html4/loose.dtd">
   <%@page import="javax.naming.InitialContext"%>
   <%@page import="demo.HelloWorld"%>
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Hello World</title>
   </head>
   <body>
   <%
       InitialContext ctx = new InitialContext();
       HelloWorld message =  (HelloWorld)ctx.lookup("HelloWorldStateless/remote");
   %>
   <%= message.hello("en")%>
   </body>
   </html>
  • Step 3 - Make sure that your Web project is also targeted to JBoss. To check this go to project properties window and check the JBoss v5.0 as one of the targeted runtimes.
  UC1d-TargetedServers.png
  • Step 3 - Select test.jsp file. Right click to choose Run On>Run On Server to test your code. WTP should deploy both Web and EJB modules to the JBoss v5.0 server, start it and display the Hello World message that comes from the EJB using the internal browser.
  File:UC1d-StartingServer.png


UC-1(e) EAR5 Combining EJB3 /WEB CLIENT USE CASE

This use case is a simple Enterprise Application v5.0 that combines the previous EJB3 application scenario and the Web Client in an EAR file, targeting it to a JEE5 runtime, and testing it.


  • Step 1 - Go to Installed Runtimes and add JBoss v5.0 as a runtime.
    EJB 3.0, and all other WTP JEE5 support should work with any server runtime
    that has JEE 5  capability such as JBoss v5, IBM WAS, BEA WebLogic Server, 
    Oracle AS, JOnAS, Apache Geronimo among many others, although there maybe 
    some delays in providing server adapters for all.
   UC1c-CreateNewRuntime-1.png
   UC1c-CreateNewRuntime-2.png
  • Step 2 - Create a new Enterprise Application Project named HelloEar, select JBoss v5.0 as the target runtime
  File:UC1d-NewEARProject.png
  • Step 3 - Select EAR 5.0 click next to accept defaults.
  UC1d-NewEarFacets.png
  • Step 4 - Click Next. In the next wizard page, you have the option to specify the mdoules in the EAR module. Choose HelloWorldEJB and HelloWorldWeb. If you have started this usecase from scratch, you can create new Web and EJB3 modules and follow the steps described in th eprevious usecases to develop them. Do NOT check the option to create a Deployment Descriptor, EAR 5.0 modules can figure out what is in an EAR module automatically. Click Finish.
UC1d-GenerateDeploymentDescriptor.png


  • Step 5 - The global JNDI name for the EJB3 changes when an EJB is packaged in an EAR file. Each application server may have its own way of naming the EJBs. Our JSP file in the Web client needs to be edited. For JBoss v5.0, make sure that the code looks like the following:
   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       
       "http://www.w3.org/TR/html4/loose.dtd">
   <%@page import="javax.naming.InitialContext"%>
   <%@page import="demo.HelloWorld"%>
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Hello World</title>
   </head>
   <body>
   <%
       InitialContext ctx = new InitialContext();
       HelloWorld message =  (HelloWorld)ctx.lookup("HelloEar/HelloWorldStateless/remote");
   %>
   <%= message.hello("en")%>
   </body>
   </html> 
  • Step 6 - Choose the test.jsp, and choose Run On>Run On Server, WTP will deploy the Ear module to the server, start it, open the JSP file and you sill see the Hello World Message.

UC1e-DeployEar.png


   JBoss v5.0 BUG: If you are using JBoss v5.0 fo testing this use case.  There is a JBoss v5.0 bug that prevents WTP 
   from redeploying an EJB jar, ear or a war file to JBoss (see http://jira.jboss.com/jira/browse/JBAS-4310).  As a 
   workaround stop the JBoss v5.0 server before you start this use case.


EXTENDED USE CASES (JPA 1.0 + JSF 1.2)

These scenarios extend the use cases described in the first section by adding JPA 1.0 facet to the EJB3 module to persist HelloWorldMessage and using JSF 1.2 for the Web client. They build on the last scenario that combines the EJB3 module and the Web application using an EAR file.


JPA 1.0 use case is and adaptation of the Dali Tutorial. This use case is about building and deploying dynamic web application for an order-entry system using JPA 1.0 and JSF 1.1. The complete application is packaged as a web application.

EJB3 with JPA use case involves creating an EJB 3.0 stateless session bean, that uses JPA 1.0 to map a simple class. A web application using JSF is the EJB client. Whole application is packaged as an EAR.

UC-2(a) EAR5 Combining JPA version of EJB3 /WEB CLIENT USE CASE

This use case is the most creates a simple POJO mapped using JPA 1.0. A stateless session references the entity manager to query the DB. The EJB jar is packaged in an EAR module

  • Step 1 - Go to Installed Runtimes and add Glassfish as a runtime.
    EJB 3.0, and all other WTP JEE5 support should work with any server runtime
    that has JEE 5  capability such as JBoss, IBM WAS, BEA WebLogic Server, 
    Oracle AS, JOnAS, Apache Geronimo among many others, although there maybe 
    some delays in providing server adapters for all.
  • Step 2 - If you have not done so, create an EJB3 module, a Web module and include them in an EAR module as described in the previous use cases. Follow the step to develop the testçjsp and the HelloWorld EJB.
  • Step 4 - Create a connection to it and create a new table using the following script:
   CREATE TABLE HelloWorldMessage(
       ID varchar(10) NOT NULL,
       MESSAGE varchar(50) NOT NULL,
       LANGUAGE varchar(50) NOT NULL,
       PRIMARY KEY(ID)
       );
  • Step 5 - Add a few rows of data such as
   1 Hello World! en
   2 Merhaba Dünya tr
  • Step 6 - Choose project properties to add a new Facet. Choose Java Persistence version 1.0. Click Next.

UC2a-JPAFacet.png

  • Step 6 - In the modify facet page, choose the generic platform and the database connection you have created. You will have two choices for JPA implementation, the default one that comes with the server runtime (JBoss v5.0), or choose a specific implementation library. You can use the links below the selection box to define new ones if you have not done so already. Choose Click Next.

UC2a-ModifyFacet-step1.png

  • Step 6-Optional- Click on Configure user libraries to define TOPLink Essentials as JPA implementation. Add TOPLink jar and click OK. When you are done choose it as the default JPA library.

UC2a-ModifyFacet-step2.png

  • Step 7 - Choose Window>Open Perspective and switch to the JPA Development perspective.

UC2a-SwitchJPAPerspective.png

  • Step 8 - Edit the HelloWorldMessage class using the Java editor in the JPA Development perspective.

UC2a-JPAPerspective.png

  • Step 9 - In the Java editor click on the HelloWorldMessage class text. In the JPA Details view, select Map As: Entity, table name and schema to reflect our choices for this class.

UC2a-ClassJPADetail.png

  • Step 10 - In the JPA outline click on the attribute id. In the JPA Details view, select Map As: Id.

UC2a-IDJPADetail.png

  • Step 11 - When you are done HelloWorldMessage class will look like the following:


   @Entity
   @Table(schema="DALI", name = "HELLOWORLDMESSAGE")
   public class HelloWorldMessage implements Serializable {
       private static final long serialVersionUID = 1L;
       @Id
       private Long id;
       private String language;
       private String message;
         ...

(Note: We had to add an @Table(schema="DALI", name="HELLOWORLDMESSAGE") in order to get this working.)

  • Step 8 - Open your persistence.xml in the META-INF folder. (create one if it does not exists in….) file, and make sure that it looks like:
   <?xml version="1.0" encoding="UTF-8"?>
   <persistence version="1.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_1_0.xsd">
       <persistence-unit name="HelloWorldPU">
           <jta-data-source>jdbc/MyDataSource</jta-data-source>
           <mapping-file>META-INF/orm.xml</mapping-file>
       </persistence-unit>
   </persistence>
  • Step 9 - Refer to Application Server manuals to create a new DataSource to the Derby database with the JNDI name “jdbc/MyDataSource”.

(Note: In JBoss you create an XML file in the server/default/deploy folder. In Glassfish, bring up the Admin Console for Glassfish by pointing to http://localhost:4848 and entering userid: admin password: adminadmin)

  • Step 10 - Update the code for your EJB3 stateless session bean implementation HelloWorldBean, and make sure that it looks like the following:
   @Stateless(name = "HelloWorldStateless", mappedName = "HelloWorldStateless")
   @Remote(HelloWorld.class)
   public class HelloWorldBean implements HelloWorld {
       @PersistenceUnit(name = "HelloWorldPU")
       EntityManagerFactory emf;
       public HelloWorldMessage hello(String language) {
           Query query = emf.createEntityManager().createQuery(
                   "select h from HelloWorld h where h.language = :lang ");
           query.setParameter("lang", language);
           List<HelloWorldMessage> list = query.getResultList();
           if (list.size() == 0)
               return list.get(0);
           return null;
       }
   }
  • Step 11 - You are done with creating an EJB 3.0 Module. Next you will create a client Web application.
  • Step 12 - There is no need to change the Web module now. Select test.jsp, Run As > Run On Server, the ear should be packaged and deployed with EJB and WAR, page should display the message.


UC-2(b) EAR5 Combining JPA version of EJB3 / JSF version WEB CLIENT USE CASE

(Note: THIS SCENARIO HAS NOT BEEN UPDATED YET BECAUSE WE FAIL TO EXECUTE IT WITH THE LATEST DRIVER)

  • Step 12 - Create a new Dynamic Web Project (Web 2.5, Java 5, JSF 1.1) named HelloWorldWeb, select GlassFish as the target runtime. Make sure that HelloWorldWeb is also added to the HelloWorldEAR.
  • Step 13 - Select Dynamic Web Module 2.5, and Java 5, JSF 1.1 as facets, click next to accept defaults. A new Web 2.5 project is created.
  • Step 14 - Go to module dependencies and add a dependency to HelloWorldEJB from HelloWorldWeb
  • Step 15 - Add a JSF backing bean called HelloWorldBean as follows:
        public class HelloWorldBean 
            implements Serializable {
          String language= “en”;
          HelloBeanFacade helloBean;
          public String hello(){
              if(helloBean == null)
                init();
              return helloBean.hello(getLanguage);
          }
          public void  setLanguage(..)
          public String getLanguage(..)
          private void init() {
              try {
                //JSF BEANS DO NOT SUPPORT INJECTION WITH
                // ANNOTATIONS so use Naming Context
                InitialContext ctx = new InitialContext();
                helloBean = (HelloBeanFacade) 
                ctx.lookup("HelloWorldStateless");      
              } catch (Exception e) {
                // Handle exception ...
              }
           }
        }
  • Step 16 - Add the following managed bean to the faces-config.xml
        <faces-config>
          <managed-bean>
            <description>hello world</description>
            <managed-bean-name>helloBean</managed-bean-name>
            <managed-bean-class>
              demo.web.HelloWorldBean
            </managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
          </managed-bean>
          ... ...
  • Step 17 - Create a new file named hello.jsp. JSP file should look like:
        <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
        <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
        <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Hello World</title>
        <head>
        <body>
        <f:view>
          <h:form> 
                <h:outputText value="#{helloBean.hello}"/>
                <h:selectOneMenu onchange="submit()"  
                  value="#{helloBean.language}" immediate="true">
                     <f:selectItem itemLabel="en" itemValue="en"/>
                     <f:selectItem itemLabel="tr" itemValue="tr"/>
                </h:selectOneMenu>
           </h:form>
        </f:view>
        </body>
        </html>
  • Step 18 - Select hello.jsp, Run As > Run On Server, the ear should be packaged and deployed with EJB and WAR, page should display the message.

Back to the top