SlideShare una empresa de Scribd logo
1 de 27
Descargar para leer sin conexión
JSF, JPA, and EJB




      Will Provost
      Ken Kousen



       Version 5.0
JSF+EJB. JSF, JPA, and EJB
Version 5.0



Information in this document is subject to change without notice. Companies, names
and data used in examples herein are fictitious unless otherwise noted. No part of this
document may be reproduced or transmitted in any form or by any means, electronic or
mechanical, for any purpose, without the express written permission of the author.

A publication of Capstone Courseware, LLC.
877-227-2477
www.capstonecourseware.com

© 2007 Will Provost. All rights reserved.
Published in the United States.

This book is printed on 100% recycled paper.




© 2007 Will Provost.                                                                 2
All Rights Reserved by Capstone Courseware, LLC.
Introduction
• This presentation illustrates the use of three Java-EE-5.0 APIs
  in concert:
    − JavaServer Faces, or JSF – version 1.2
    − Enterprise JavaBeans, or EJB – version 3.0
    − The Java Persistence API, or the JPA
• Though it may be interesting on its own (to those with
  experience in one or more of the above standards), we’ve built
  it primarily to compliment Capstone courses on these subjects.
    − It will be most effective as a sort of encore presentation at the end
      of a class that uses one or both of those courses.
    − It uses a build-and-deploy infrastructure almost identical to that
      found in the EJB course, and requires the same application server.
• We discuss each of these three APIs very briefly before
  addressing the issues involved in integrating them.




© 2007 Will Provost.                                                     3
All Rights Reserved by Capstone Courseware, LLC.
JavaServer Faces
• JavaServer Faces represents a fresh approach to the problem
  of modeling and implementing web user interfaces.
                                             JSF Runtime Classes           Application Classes

                                           UIViewRoot
          JSP
                                                   HtmlForm
  <f:view>
   <h:form>                                         HtmlInputText
    <h:inputText/>
    <h:inputText/>
    <h:dataTable>                                   HtmlInputText
     <h:column>
      <...Checkbox/>                                HtmlDataTable                  Backing
      <h:inputText/>
     </h:column>                                                                    Bean
    </h:dataTable>                                      HtmlColumn
    <h:commandButton/>
   </h:form>                                                H...Checkbox
  </f:view>
                                                           HtmlInputText
                                                                                   Action
                                                      H...Button
                                                                                  Listener

                          Browser       Web Server

    − JSP (or other technology) defines a component tree that has as
      much to do with AWT/JFC GUI-building as it does with traditional
      request/response and web-MVC frameworks.
    − Components rely on backing beans for presentation state and
      business logic (state and behavior).




© 2007 Will Provost.                                                                         4
All Rights Reserved by Capstone Courseware, LLC.
Enterprise JavaBeans
• Enterprise JavaBeans defines a standard for scalable
  business and data-access objects that relies on a dedicated
  container to:
    − Manage component lifecycle
    − Mediate conversations between clients and contained objects –
      and between multiple contained objects – so as to implement
      various enterprise features on behalf of the objects
• EJBs come in three flavors:
    − Session beans are front-line business objects, and these are
      further classified as stateful or stateless with regard to the client
      conversations or use cases that they implement.
    − Entity beans – or, in the newer parlance, simply entities – are
      persistent state objects; they act as DAOs but can also exist in
      transient and detached states and so are usable in business and
      even presentation tiers.
    − Message-driven beans exist specifically to handle incoming
      messages by asynchronous systems such as the Java Message
      Service (JMS).
• EJB relies heavily on metadata to inform the container as to
  how a given bean should be hosted and handled; this metadata
  may be provided in either or both of two forms:
    − Annotations in the Java source code
    − XML deployment descriptors external to the Java class(es)




© 2007 Will Provost.                                                     5
All Rights Reserved by Capstone Courseware, LLC.
The Java Persistence API
• Part of the EJB 3.0 specification is known as the Java
  Persistence API, or JPA.
    − The EJB 3.0 specification is split into three separate documents,
      one of which defines the JPA.
    − In its next iteration, the JPA will become a stand-alone
      specification for persistence in both Java SE and Java EE
      applications.
    − It is already usable by Java SE applications, using the Java EE JARs
      but not requiring the use of an EJB container.
• The JPA incorporates into Java EE a full object/relational
  mapping (ORM) layer.

     Component                               Java Persistence API


                                         ORM/Persistence Provider


                                                    JDBC


    − Persistence providers will be tools such as TopLink or Hibernate.




© 2007 Will Provost.                                                      6
All Rights Reserved by Capstone Courseware, LLC.
The Big Picture
• So a multi-tier, Java-EE application might take advantage of all
  of this technology:
 Web                                                          EJB
 Container                                                    Container
                                                   Backing    ?   Session
                                                    Backing        Backing
                                                    Beans          Beans
                                                     Beans          Beans
     <f:view>
      ...
      ...
     </f:view>
                                                         ?        Entities
                                                                   Entities



    − JSF helps us meet the challenges of the presentation tier.
    − EJBs, and specifically session beans, may be our business tier.
    − JPA entities make up a persistence tier.
    − Not shown in the diagram but part of the upcoming examples are
      JMS and message-driven beans, integrating with the session
      beans to connect to the same business and persistence logic.
• The rest of this presentation discusses the challenges of
  combining this technology – specifically:
    − Using session beans from a JSF web tier, such that the business
      tier of the application is hosted by the EJB container, as shown
    − Using JPA entities directly from JSF – in this case the business
      logic is implemented in POJOs and runs in the web container




© 2007 Will Provost.                                                          7
All Rights Reserved by Capstone Courseware, LLC.
Using Annotations in JSF
• Most of the connectivity between JSF and either EJB or JPA will
  be implemented by expressing the appropriate metadata to the
  web container.
• Essentially, we’re talking about dependency injection: the
  ability of a container to connect a dependent component to a
  collaborating component based on metadata.
    − A Java-EE-5.0 web container is aware of the full set of annotations
      defined for use with EJB, JPA, and other APIs.
    − XML metadata is also possible – and even preferred in some cases,
      as we discuss in our EJB coursebook.
• Only certain managed objects can take advantage of
  dependency injection.
    − This is true for the web as well as the EJB container, though with
      the latter it’s less obvious since most hosted components are EJBs!
• Specifically, in JSF, one may define injectable dependencies in
  the following places:
    − Servlets
    − Servlet filters
    − Web context, request, and session event listeners
    − Custom JSP tag handlers
    − JSF managed beans
• It’s the last of these that will be of the greatest interest to most
  JSF application developers.


© 2007 Will Provost.                                                   8
All Rights Reserved by Capstone Courseware, LLC.
EJB Annotations
• A JSF managed bean can make calls on an EJB most easily by:
    − Defining a field of that EJB’s type
    − Annotating that field, or the corresponding mutator method, as a
      reference to an @EJB
@EJB private MyBeanType myBean;

• The usual range of dependency-injection rules and options
  obtain for a JSF managed bean.
    − Injection by type will work when only one implementation (class)
      of the given bean type (interface) is found by the container.
    − Injection by name will be necessary otherwise, and here the
      syntax gets a bit more involved, using either a beanName
      attribute on the annotation ...
@EJB (beanName="Fred") private MyBeanType myBean;

    − ... or a name attribute that connects to XML metadata:
@EJB (name="mine") private MyBeanType myBean;

<ejb-ref>
  <ejb-ref-name>mine</ejb-ref-name>
  <ejb-ref-type>Session</ejb-ref-type>
  <remote>BeanType</remote>
  <ejb-link>Fred</ejb-link>
</ejb-ref>

    − Both local and remote references are possible.




© 2007 Will Provost.                                                9
All Rights Reserved by Capstone Courseware, LLC.
Using Stateful Session Beans
• The above technique is actually appropriate only for stateless
  session beans, because a client needs only one reference to a
  stateless bean, even if it will make many calls to that bean.
• Stateful beans won’t work this way.
    − Whatever injection method we choose, we will still have one
      reference to one bean.
    − How can we wedge all our different client conversations – our own
      sessions – into that one object? It will surely become confused!
• For a stateful bean, the general technique is to use the servlet
  context to look up the stateful bean each time you need it:
((ServletContext)
  FacesContext.getCurrentInstance ()
    .getExternalContext ().getContext ())
      .lookup ("thatStatefulBean");

    − This requires an <ejb-local-ref> or <ejb-ref> just as used in
      ordinary injection by name.
    − This will trigger creation of a distinct bean for your session;
      typically this reference would be saved at session scope in the web
      application as well.
• In our upcoming example, we use a stateless bean that acts as a
  factory for stateful beans.
    − This simplifies life for the web application, as it can use a simple
      @EJB annotation and then call the factory method as needed.




© 2007 Will Provost.                                                     10
All Rights Reserved by Capstone Courseware, LLC.
Tools and Environment
• For our practical exercises with EJB, we’ll rely on the Java EE
  5.0 SDK from Sun Microsystems.
• The Java EE 5.0 SDK must be installed on your system, typically
  in c:SunSDK.
• Other tools are deployed with the example code, under
  c:/Capstone/Tools:
    − The JSTL libraries are in JSTL1.1.
    − MySQL and its JDBC driver are in MySQL5.0.
• Be sure that the following environmental settings are correct:
    − The executable path includes the bin and jdk/bin directories
      under the SDK installation root.
    − The environment variable JAVA_HOME is set to the jdk directory.
    − The environment variable JAVA_EE_HOME is set to the SDK
      installation root itself.
    − An environment variable CC_MODULE is set to
      c:CapstoneJSF+EJB.
• Copy the file mysql-connector-java-5.0.4-bin.jar from
  c:/Capstone/Tools/MySQL5.0/lib to
  %JAVA_EE_HOME%/lib.
    − This will assure that the Java EE server can find the MySQL JDBC
      driver on the server’s own classpath.




© 2007 Will Provost.                                                 11
All Rights Reserved by Capstone Courseware, LLC.
Starting the MySQL and Java EE Servers
• To start the MySQL RDBMS, simply run the following
  command from c:/Capstone/Tools/MySQL5.0/bin:
mysqld

    − Note that the console you use to run this command will appear to
      hang; this is the MySQL daemon process listening for connections
      at its default port 3306.
• When you want to shut the server down again, run this
  prepared script from the same directory:
shutdown

• Start the application server from the Windows Start menu, as
  shown below ...




    − You’ll see confirmation that the server is running and ready for
      requests, and a prompt to hit any key – do so.
• You can stop the server from the menu – but leave it running
  for now.




© 2007 Will Provost.                                                     12
All Rights Reserved by Capstone Courseware, LLC.
The LandUse Application                                        EXAMPLE

• In c:/Capstone/JSF+EJB/Examples/LandUse, there is a
  version of the LandUse application in which a JSF GUI uses
  session and entity beans by way of dependency injection to a
  managed bean.
• In this presentation we will focus on the bridge between JSF
  and EJB, and see a summary of their interactions.
• We leave details of the rest of the application for further study.
    − The JSF code is almost identical to that found in the JSF course.
    − The same can be said of the EJBs and relational database as found
      in the EJB course.
• The bridge between the web tier and EJB tiers of this
  application is found in the dependencies from a single managed
  bean to a stateless session bean and a stateful session bean:




© 2007 Will Provost.                                                      13
All Rights Reserved by Capstone Courseware, LLC.
The LandUse Application                                        EXAMPLE

• First, consider the JSF configuration file, found in
  docroot/WEB-INF/faces-config.xml:
<managed-bean>
  <managed-bean-name>DB</managed-bean-name>
  <managed-bean-class>
    gov.usda.usfs.landuse.web.DatabaseWrapper
  </managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

    − The managed bean DB is the gateway to the business logic and
      persistent data for the application.
    − In the JSF-only version of this application, this bean manages the
      loading and saving of a file of serialized objects.
    − Here, it will connect to EJBs.




© 2007 Will Provost.                                                  14
All Rights Reserved by Capstone Courseware, LLC.
The LandUse Application                                         EXAMPLE

• In
  src/gov/usda/usfs/landuse/web/DatabaseWrapper.java,
  see the simple injection-by-type use of the @EJB annotation:
@EJB private LandUseService landUseService;

• This session-scope bean uses the stateless bean to get “global”
  information such as the list of proposals in the database.
private String loadData ()
{
  List<Proposal> proposals =
    landUseService.getAll ();
  ...
}

    − And how are we assured that this method is invoked when the
      bean is created?
    − The Java-EE annotation @PostConstruct assures that the init
      method will be called; this calls loadData.
@PostConstruct
public void init ()
{
  loadData ();
}

    − This method is akin to a constructor, but will be called only after
      injected dependencies have been set.




© 2007 Will Provost.                                                    15
All Rights Reserved by Capstone Courseware, LLC.
The LandUse Application                                        EXAMPLE

• Then, when the user begins working with a specific proposal,
  the managed bean derives a reference to the stateful session
  bean – see the edit method:
public String edit ()
{
  proposalService = null;
  for (int i = 0; i < data.size (); ++i)
    if (data.get (i).isSelected ())
    {
      proposalService =
        landUseService.getServiceForProposal
          (data.get (i).getProposal ().getID ());
      selectedProposal = proposalService.get ();
      return "detail";
    }
    ...

    − When the Done or Decide button is clicked, the managed bean
      throws away its reference to the stateful bean with a call to the
      helper method clearDetailState, and returns to the summary
      page.
private void clearDetailState ()
{
  proposalService = null;
  possibleDecision = new Decision ();
  possibleComment = new Comment ();
}




© 2007 Will Provost.                                                      16
All Rights Reserved by Capstone Courseware, LLC.
The LandUse Application                                          EXAMPLE

• Set up, build, and deploy the application as follows:
1. If you’ve deployed any of the LandUse steps from the EJB course,
   clean them up first:
asant clean-DB

2. Create the underlying database of land-use proposals, and establish
   JDBC resources for the application server:
asant init-DB

3. Build the application and prime the database using a simple Java SE
   console application:
asant build
run PrimeWithData

4. Deploy the EAR file that holds the web and EJB tiers of the
   application:
asant deploy

• The full console output from this series of commands (starting
  with asant init-DB) can be found in ConsoleLog.txt.




© 2007 Will Provost.                                                  17
All Rights Reserved by Capstone Courseware, LLC.
The LandUse Application                                     EXAMPLE

• Open the following URL in your web browser:
http://localhost:8080/LandUse




    − To produce this page, the JSF component tree derives data from
      the managed bean – which in turn calls getAll on the stateless
      LandUseServiceImpl.




© 2007 Will Provost.                                                   18
All Rights Reserved by Capstone Courseware, LLC.
The LandUse Application                                       EXAMPLE

• Choose one of the proposals, and click Edit:




    − This is where the stateful bean is brought into play, and from this
      point forward the (session-scope) managed bean will use the same
      stateful bean for its operations: adding comments, changing basic
      data, or rendering decision.




© 2007 Will Provost.                                                  19
All Rights Reserved by Capstone Courseware, LLC.
Impact on the JSF Application
• A few significant changes were required on the JSF side to work
  with EJBs instead of a private file of serialized objects.
    − In fact most of these have more to do with moving to a multi-tier
      architecture than with EJB specifically; the EJB impacts are really
      no more and no less than those mentioned in the example.
• State management in the web tier is a little different.
    − The JSF-only application tracks the selected proposal for a user
      session as an ordinal value into a List.
    − This is fine when (a) we have the list to ourselves so it’s not
      volatile, and (b) ordinal values are meaningful!
    − For a database, ordinal values are usually unreliable, and instead
      it’s an ID or primary key that matters.
    − So the managed bean tracks its selectedProposal as an object
      reference, along with the stateful-bean reference proposalService.
• One simple but critical change that is specific to Java EE: the
  web.xml must declare that the application uses Servlets 2.5.
    − JSF can be configured for a version-2.4 application, but
      dependency injection is a 2.5 feature.
    − Change the version back to 2.4, and watch the connection to the
      stateless session bean quietly fail to materialize.




© 2007 Will Provost.                                                     20
All Rights Reserved by Capstone Courseware, LLC.
JPA Annotations
• With a full Java-EE library on hand – but even without an EJB
  container – Java code can take direct advantage of the JPA.
    − Thus EJBs are not essential to JPA persistence, but rather one
      means of implementing that persistence.
• Implementing a persistent type using JPA involves a wide range
  of annotations beginning with @Entity and also including
  definitions of table and column mappings, association
  cardinality, and eager vs. lazy fetching strategies.
    − This is beyond our scope.
• To use a JPA entity class to carry out database operations is a
  simpler matter:
    − The entry point is an object of type EntityManager.
    − It represents an abstraction known as the persistence context.
    − A persistence context is an instance of a persistence unit.
• A persistence.xml file will define one or more persistence
  units.
    − For EJB JARs this is found in the JAR’s META-INF directory.
    − For standalone applications it is found in a META-INF directory
      anywhere off the class path.
    − For web applications it goes in the WEB-INF directory.
• The JPA implementation will find this definition and seek out a
  context when the application attempts to create an
  EntityManager.


© 2007 Will Provost.                                                    21
All Rights Reserved by Capstone Courseware, LLC.
Using JPA from Web Components
• When EJBs use JPA entities, they typically adopt a fully
  declarative strategy by which the EJB container manages
  transaction boundaries and persistence contexts.
    − Transaction and context will live for a single method call.
    − The bean can declare different requirements, such as no
      transactions or a context that lives longer than a method call.
• For web components, things are different:
    − Web containers will not manage transactions.
    − There is no analog to the per-method scope for persistence
      contexts; and we often need longer durations such as for the life of
      a user session.
• Therefore a JSF application adopts a different strategy – partly
  declarative and using dependency injection, but governing
  transaction and persistence-context boundaries manually.
    − Declare an injectable EntityManagerFactory using the
      @PersistenceUnit annotation.
    − Create and close EntityManagers as needed.
    − A sound approach uses lifecycle methods on a managed bean,
      identified by @PostConstruct and @PreDestroy annotations.
    − Control transaction boundaries using the EntityTransaction
      interface, an instance of which is exposed by EntityManager.




© 2007 Will Provost.                                                    22
All Rights Reserved by Capstone Courseware, LLC.
The Human Resources Application                       EXAMPLE

• In c:/Capstone/JSF+EJB/Examples/HR there is a simple JSF
  application that shows department and employee records from
  a human-resources database, and allows limited data editing.
• A single session-scope bean of type DatabaseWrapper
  invokes persistence methods to manipulate JPA entities for
  Department and Employee records:




© 2007 Will Provost.                                           23
All Rights Reserved by Capstone Courseware, LLC.
The Human Resources Application                               EXAMPLE

• See src/cc/hr/web/DatabaseWrapper.java: this class
  coordinates all JPA activities for the application.
    − It defines an injectable EntityManagerFactory and also an
      EntityManager that it will manage by itself:
@PersistenceUnit
private EntityManagerFactory factory;

private EntityManager mgr;

    − It controls the creation and closing of the EntityManager in its
      lifecycle methods; so we know that the persistence context will be
      in force for the user session:
@PostConstruct
public void createEntityManager()
{
  mgr = factory.createEntityManager();
}

@PreDestroy
public void closeEntityManager()
{
  mgr.close();
}




© 2007 Will Provost.                                                  24
All Rights Reserved by Capstone Courseware, LLC.
The Human Resources Application                             EXAMPLE

    − It uses the EntityManager for queries and updates, wrapping the
      updates in an EntityTransaction:
public List<Department> getDepartments ()
{
  data = (List<Department>) mgr.createQuery
    ("select x from Department x").getResultList ();
  return data;
}

public void update ()
{
  EntityTransaction tx = mgr.getTransaction ();
  for (Department department : data)
    try
    {
      tx.begin ();
      mgr.merge (department);
      tx.commit ();
    }
    catch (Exception ex)
    {
      System.out.println ("Exception during " +
        "transaction -- rolling back.");
      ex.printStackTrace ();
      try { tx.rollback (); }
      catch (Exception ex2) {}
    }
}




© 2007 Will Provost.                                               25
All Rights Reserved by Capstone Courseware, LLC.
The Human Resources Application                        EXAMPLE

• See docroot/WEB-INF/classes/META-INF/persistence.xml,
  which defines the JPA persistence unit for the application:
<persistence-unit name="Earthlings"
  transaction-type="RESOURCE_LOCAL" >
  <provider>
    oracle.toplink.essentials.PersistenceProvider
  </provider>
  <jta-data-source>
    jdbc/Earthlings
  </jta-data-source>
  <properties>
    <property name="toplink.logging.level"
               value="INFO" />
  </properties>
</persistence-unit>

• Set up, build, and test the application as follows. (MySQL and
  the Java EE application server should both still be running.)
1. Build the database:
asant create-DB

2. Build and deploy the application:
asant




© 2007 Will Provost.                                           26
All Rights Reserved by Capstone Courseware, LLC.
The Human Resources Application                                  EXAMPLE

3. Test at the following URL:
http://localhost:8080/HR




4. Try changing one or more employee salaries, and see that the
   department payroll accurately reflects the updated salaries. This is
   persistent now, so new sessions, redeploys of the application, etc., all
   will see the durable data from the MySQL database.




© 2007 Will Provost.                                                     27
All Rights Reserved by Capstone Courseware, LLC.

Más contenido relacionado

La actualidad más candente (20)

15 expression-language
15 expression-language15 expression-language
15 expression-language
 
WEB TECHNOLOGIES JSP
WEB TECHNOLOGIES  JSPWEB TECHNOLOGIES  JSP
WEB TECHNOLOGIES JSP
 
Ejb3 Presentation
Ejb3 PresentationEjb3 Presentation
Ejb3 Presentation
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
 
EJB .
EJB .EJB .
EJB .
 
Intershop bo
Intershop boIntershop bo
Intershop bo
 
enterprise java bean
enterprise java beanenterprise java bean
enterprise java bean
 
JEE Course - EJB
JEE Course - EJBJEE Course - EJB
JEE Course - EJB
 
Free EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggetsFree EJB Tutorial | VirtualNuggets
Free EJB Tutorial | VirtualNuggets
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
 
EJB Interview Questions
EJB Interview QuestionsEJB Interview Questions
EJB Interview Questions
 
Enterprise Java Beans( E)
Enterprise  Java  Beans( E)Enterprise  Java  Beans( E)
Enterprise Java Beans( E)
 
J2EE and Servlet
J2EE and Servlet J2EE and Servlet
J2EE and Servlet
 
EJB 2
EJB 2EJB 2
EJB 2
 
Enterprise java beans
Enterprise java beansEnterprise java beans
Enterprise java beans
 
Ch4 ejb
Ch4 ejbCh4 ejb
Ch4 ejb
 
Session 1 Tp1
Session 1 Tp1Session 1 Tp1
Session 1 Tp1
 
GIDS 2012: Java Message Service 2.0
GIDS 2012: Java Message Service 2.0GIDS 2012: Java Message Service 2.0
GIDS 2012: Java Message Service 2.0
 
EJB 3.1 by Bert Ertman
EJB 3.1 by Bert ErtmanEJB 3.1 by Bert Ertman
EJB 3.1 by Bert Ertman
 
0012
00120012
0012
 

Destacado

2010 12 02 Presentatie Kcc Congres 2010 Brw Groep
2010 12 02 Presentatie Kcc Congres 2010 Brw Groep2010 12 02 Presentatie Kcc Congres 2010 Brw Groep
2010 12 02 Presentatie Kcc Congres 2010 Brw Groepedevoogd
 
Webcare met HowAboutYou bij Circle Software Relatiedag 2012
Webcare met HowAboutYou bij Circle Software Relatiedag 2012Webcare met HowAboutYou bij Circle Software Relatiedag 2012
Webcare met HowAboutYou bij Circle Software Relatiedag 2012edevoogd
 
2011 expoward primaria 6to. año c6
2011 expoward primaria 6to. año c62011 expoward primaria 6to. año c6
2011 expoward primaria 6to. año c6nm48
 
2010 10 26 Presentatie Leveranciers Kcs Onderzoek 2010
2010 10 26 Presentatie Leveranciers Kcs Onderzoek 20102010 10 26 Presentatie Leveranciers Kcs Onderzoek 2010
2010 10 26 Presentatie Leveranciers Kcs Onderzoek 2010edevoogd
 
2013 10 31 webcare en online media monitoring bij gemeenten met howaboutyou
2013 10 31 webcare en online media monitoring bij gemeenten met howaboutyou2013 10 31 webcare en online media monitoring bij gemeenten met howaboutyou
2013 10 31 webcare en online media monitoring bij gemeenten met howaboutyouedevoogd
 
Online media monitoring en webcare bij gemeenten
Online media monitoring en webcare bij gemeentenOnline media monitoring en webcare bij gemeenten
Online media monitoring en webcare bij gemeentenedevoogd
 
2011 expoward primaria 6to. año c2
2011 expoward primaria 6to. año c22011 expoward primaria 6to. año c2
2011 expoward primaria 6to. año c2nm48
 
Classi_astratte_interfacce
Classi_astratte_interfacceClassi_astratte_interfacce
Classi_astratte_interfaccelullabyte
 
Jan de kramer antwoord2
Jan de kramer antwoord2Jan de kramer antwoord2
Jan de kramer antwoord2edevoogd
 
Grad survey results presentation
Grad survey results presentationGrad survey results presentation
Grad survey results presentationCarissa Caloud
 
Opening Up New tool New changes
Opening Up New tool New changesOpening Up New tool New changes
Opening Up New tool New changesedevoogd
 
2010 10 27 Workshop Besparen Met Antwoord Benchmark Pu Za Brw Groep
2010 10 27 Workshop Besparen Met Antwoord Benchmark Pu Za Brw Groep2010 10 27 Workshop Besparen Met Antwoord Benchmark Pu Za Brw Groep
2010 10 27 Workshop Besparen Met Antwoord Benchmark Pu Za Brw Groepedevoogd
 
Grad survey results presentation
Grad survey results presentationGrad survey results presentation
Grad survey results presentationCarissa Caloud
 
2011 09 19 afbeeldingen publicatie
2011 09 19 afbeeldingen publicatie2011 09 19 afbeeldingen publicatie
2011 09 19 afbeeldingen publicatieedevoogd
 

Destacado (14)

2010 12 02 Presentatie Kcc Congres 2010 Brw Groep
2010 12 02 Presentatie Kcc Congres 2010 Brw Groep2010 12 02 Presentatie Kcc Congres 2010 Brw Groep
2010 12 02 Presentatie Kcc Congres 2010 Brw Groep
 
Webcare met HowAboutYou bij Circle Software Relatiedag 2012
Webcare met HowAboutYou bij Circle Software Relatiedag 2012Webcare met HowAboutYou bij Circle Software Relatiedag 2012
Webcare met HowAboutYou bij Circle Software Relatiedag 2012
 
2011 expoward primaria 6to. año c6
2011 expoward primaria 6to. año c62011 expoward primaria 6to. año c6
2011 expoward primaria 6to. año c6
 
2010 10 26 Presentatie Leveranciers Kcs Onderzoek 2010
2010 10 26 Presentatie Leveranciers Kcs Onderzoek 20102010 10 26 Presentatie Leveranciers Kcs Onderzoek 2010
2010 10 26 Presentatie Leveranciers Kcs Onderzoek 2010
 
2013 10 31 webcare en online media monitoring bij gemeenten met howaboutyou
2013 10 31 webcare en online media monitoring bij gemeenten met howaboutyou2013 10 31 webcare en online media monitoring bij gemeenten met howaboutyou
2013 10 31 webcare en online media monitoring bij gemeenten met howaboutyou
 
Online media monitoring en webcare bij gemeenten
Online media monitoring en webcare bij gemeentenOnline media monitoring en webcare bij gemeenten
Online media monitoring en webcare bij gemeenten
 
2011 expoward primaria 6to. año c2
2011 expoward primaria 6to. año c22011 expoward primaria 6to. año c2
2011 expoward primaria 6to. año c2
 
Classi_astratte_interfacce
Classi_astratte_interfacceClassi_astratte_interfacce
Classi_astratte_interfacce
 
Jan de kramer antwoord2
Jan de kramer antwoord2Jan de kramer antwoord2
Jan de kramer antwoord2
 
Grad survey results presentation
Grad survey results presentationGrad survey results presentation
Grad survey results presentation
 
Opening Up New tool New changes
Opening Up New tool New changesOpening Up New tool New changes
Opening Up New tool New changes
 
2010 10 27 Workshop Besparen Met Antwoord Benchmark Pu Za Brw Groep
2010 10 27 Workshop Besparen Met Antwoord Benchmark Pu Za Brw Groep2010 10 27 Workshop Besparen Met Antwoord Benchmark Pu Za Brw Groep
2010 10 27 Workshop Besparen Met Antwoord Benchmark Pu Za Brw Groep
 
Grad survey results presentation
Grad survey results presentationGrad survey results presentation
Grad survey results presentation
 
2011 09 19 afbeeldingen publicatie
2011 09 19 afbeeldingen publicatie2011 09 19 afbeeldingen publicatie
2011 09 19 afbeeldingen publicatie
 

Similar a Jsf+ejb 50

J2EE - Practical Overview
J2EE - Practical OverviewJ2EE - Practical Overview
J2EE - Practical OverviewSvetlin Nakov
 
1-introduction to ejb
1-introduction to ejb1-introduction to ejb
1-introduction to ejbashishkirpan
 
Real world java_ee_patterns
Real world java_ee_patternsReal world java_ee_patterns
Real world java_ee_patternsAlassane Diallo
 
Java Enterprise Edition 6 Overview
Java Enterprise Edition 6 OverviewJava Enterprise Edition 6 Overview
Java Enterprise Edition 6 OverviewEugene Bogaart
 
Introduction to Java EE EJB Component
Introduction to Java EE EJB ComponentIntroduction to Java EE EJB Component
Introduction to Java EE EJB ComponentGanesh P
 
Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Rohit Kelapure
 
Java EE 6 & GlassFish v3 at Vancouver JUG, Jan 26, 2010
Java EE 6 & GlassFish v3 at Vancouver JUG, Jan 26, 2010Java EE 6 & GlassFish v3 at Vancouver JUG, Jan 26, 2010
Java EE 6 & GlassFish v3 at Vancouver JUG, Jan 26, 2010Arun Gupta
 
Sun Java EE 6 Overview
Sun Java EE 6 OverviewSun Java EE 6 Overview
Sun Java EE 6 Overviewsbobde
 
Java EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureJava EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureArun Gupta
 
Jsf2 overview
Jsf2 overviewJsf2 overview
Jsf2 overviewsohan1234
 
Java EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureJava EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureArun Gupta
 

Similar a Jsf+ejb 50 (20)

J2 Ee Overview
J2 Ee OverviewJ2 Ee Overview
J2 Ee Overview
 
J2EE - Practical Overview
J2EE - Practical OverviewJ2EE - Practical Overview
J2EE - Practical Overview
 
Java j2eeTutorial
Java j2eeTutorialJava j2eeTutorial
Java j2eeTutorial
 
1-introduction to ejb
1-introduction to ejb1-introduction to ejb
1-introduction to ejb
 
jsf2 Notes
jsf2 Notesjsf2 Notes
jsf2 Notes
 
Real world java_ee_patterns
Real world java_ee_patternsReal world java_ee_patterns
Real world java_ee_patterns
 
Introcution to EJB
Introcution to EJBIntrocution to EJB
Introcution to EJB
 
Java Enterprise Edition 6 Overview
Java Enterprise Edition 6 OverviewJava Enterprise Edition 6 Overview
Java Enterprise Edition 6 Overview
 
Introduction to Java EE EJB Component
Introduction to Java EE EJB ComponentIntroduction to Java EE EJB Component
Introduction to Java EE EJB Component
 
Ejb intro
Ejb introEjb intro
Ejb intro
 
JBoss AS7 OSDC 2011
JBoss AS7 OSDC 2011JBoss AS7 OSDC 2011
JBoss AS7 OSDC 2011
 
Virtual Classroom
Virtual ClassroomVirtual Classroom
Virtual Classroom
 
Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010Contextual Dependency Injection for Apachecon 2010
Contextual Dependency Injection for Apachecon 2010
 
Java EE 6 & GlassFish v3 at Vancouver JUG, Jan 26, 2010
Java EE 6 & GlassFish v3 at Vancouver JUG, Jan 26, 2010Java EE 6 & GlassFish v3 at Vancouver JUG, Jan 26, 2010
Java EE 6 & GlassFish v3 at Vancouver JUG, Jan 26, 2010
 
Sun Java EE 6 Overview
Sun Java EE 6 OverviewSun Java EE 6 Overview
Sun Java EE 6 Overview
 
Ejb intro
Ejb introEjb intro
Ejb intro
 
Java EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureJava EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for future
 
Jsf2 overview
Jsf2 overviewJsf2 overview
Jsf2 overview
 
Java EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureJava EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for future
 
Unite5-EJB-2019.ppt
Unite5-EJB-2019.pptUnite5-EJB-2019.ppt
Unite5-EJB-2019.ppt
 

Último

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Último (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Jsf+ejb 50

  • 1. JSF, JPA, and EJB Will Provost Ken Kousen Version 5.0
  • 2. JSF+EJB. JSF, JPA, and EJB Version 5.0 Information in this document is subject to change without notice. Companies, names and data used in examples herein are fictitious unless otherwise noted. No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of the author. A publication of Capstone Courseware, LLC. 877-227-2477 www.capstonecourseware.com © 2007 Will Provost. All rights reserved. Published in the United States. This book is printed on 100% recycled paper. © 2007 Will Provost. 2 All Rights Reserved by Capstone Courseware, LLC.
  • 3. Introduction • This presentation illustrates the use of three Java-EE-5.0 APIs in concert: − JavaServer Faces, or JSF – version 1.2 − Enterprise JavaBeans, or EJB – version 3.0 − The Java Persistence API, or the JPA • Though it may be interesting on its own (to those with experience in one or more of the above standards), we’ve built it primarily to compliment Capstone courses on these subjects. − It will be most effective as a sort of encore presentation at the end of a class that uses one or both of those courses. − It uses a build-and-deploy infrastructure almost identical to that found in the EJB course, and requires the same application server. • We discuss each of these three APIs very briefly before addressing the issues involved in integrating them. © 2007 Will Provost. 3 All Rights Reserved by Capstone Courseware, LLC.
  • 4. JavaServer Faces • JavaServer Faces represents a fresh approach to the problem of modeling and implementing web user interfaces. JSF Runtime Classes Application Classes UIViewRoot JSP HtmlForm <f:view> <h:form> HtmlInputText <h:inputText/> <h:inputText/> <h:dataTable> HtmlInputText <h:column> <...Checkbox/> HtmlDataTable Backing <h:inputText/> </h:column> Bean </h:dataTable> HtmlColumn <h:commandButton/> </h:form> H...Checkbox </f:view> HtmlInputText Action H...Button Listener Browser Web Server − JSP (or other technology) defines a component tree that has as much to do with AWT/JFC GUI-building as it does with traditional request/response and web-MVC frameworks. − Components rely on backing beans for presentation state and business logic (state and behavior). © 2007 Will Provost. 4 All Rights Reserved by Capstone Courseware, LLC.
  • 5. Enterprise JavaBeans • Enterprise JavaBeans defines a standard for scalable business and data-access objects that relies on a dedicated container to: − Manage component lifecycle − Mediate conversations between clients and contained objects – and between multiple contained objects – so as to implement various enterprise features on behalf of the objects • EJBs come in three flavors: − Session beans are front-line business objects, and these are further classified as stateful or stateless with regard to the client conversations or use cases that they implement. − Entity beans – or, in the newer parlance, simply entities – are persistent state objects; they act as DAOs but can also exist in transient and detached states and so are usable in business and even presentation tiers. − Message-driven beans exist specifically to handle incoming messages by asynchronous systems such as the Java Message Service (JMS). • EJB relies heavily on metadata to inform the container as to how a given bean should be hosted and handled; this metadata may be provided in either or both of two forms: − Annotations in the Java source code − XML deployment descriptors external to the Java class(es) © 2007 Will Provost. 5 All Rights Reserved by Capstone Courseware, LLC.
  • 6. The Java Persistence API • Part of the EJB 3.0 specification is known as the Java Persistence API, or JPA. − The EJB 3.0 specification is split into three separate documents, one of which defines the JPA. − In its next iteration, the JPA will become a stand-alone specification for persistence in both Java SE and Java EE applications. − It is already usable by Java SE applications, using the Java EE JARs but not requiring the use of an EJB container. • The JPA incorporates into Java EE a full object/relational mapping (ORM) layer. Component Java Persistence API ORM/Persistence Provider JDBC − Persistence providers will be tools such as TopLink or Hibernate. © 2007 Will Provost. 6 All Rights Reserved by Capstone Courseware, LLC.
  • 7. The Big Picture • So a multi-tier, Java-EE application might take advantage of all of this technology: Web EJB Container Container Backing ? Session Backing Backing Beans Beans Beans Beans <f:view> ... ... </f:view> ? Entities Entities − JSF helps us meet the challenges of the presentation tier. − EJBs, and specifically session beans, may be our business tier. − JPA entities make up a persistence tier. − Not shown in the diagram but part of the upcoming examples are JMS and message-driven beans, integrating with the session beans to connect to the same business and persistence logic. • The rest of this presentation discusses the challenges of combining this technology – specifically: − Using session beans from a JSF web tier, such that the business tier of the application is hosted by the EJB container, as shown − Using JPA entities directly from JSF – in this case the business logic is implemented in POJOs and runs in the web container © 2007 Will Provost. 7 All Rights Reserved by Capstone Courseware, LLC.
  • 8. Using Annotations in JSF • Most of the connectivity between JSF and either EJB or JPA will be implemented by expressing the appropriate metadata to the web container. • Essentially, we’re talking about dependency injection: the ability of a container to connect a dependent component to a collaborating component based on metadata. − A Java-EE-5.0 web container is aware of the full set of annotations defined for use with EJB, JPA, and other APIs. − XML metadata is also possible – and even preferred in some cases, as we discuss in our EJB coursebook. • Only certain managed objects can take advantage of dependency injection. − This is true for the web as well as the EJB container, though with the latter it’s less obvious since most hosted components are EJBs! • Specifically, in JSF, one may define injectable dependencies in the following places: − Servlets − Servlet filters − Web context, request, and session event listeners − Custom JSP tag handlers − JSF managed beans • It’s the last of these that will be of the greatest interest to most JSF application developers. © 2007 Will Provost. 8 All Rights Reserved by Capstone Courseware, LLC.
  • 9. EJB Annotations • A JSF managed bean can make calls on an EJB most easily by: − Defining a field of that EJB’s type − Annotating that field, or the corresponding mutator method, as a reference to an @EJB @EJB private MyBeanType myBean; • The usual range of dependency-injection rules and options obtain for a JSF managed bean. − Injection by type will work when only one implementation (class) of the given bean type (interface) is found by the container. − Injection by name will be necessary otherwise, and here the syntax gets a bit more involved, using either a beanName attribute on the annotation ... @EJB (beanName="Fred") private MyBeanType myBean; − ... or a name attribute that connects to XML metadata: @EJB (name="mine") private MyBeanType myBean; <ejb-ref> <ejb-ref-name>mine</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <remote>BeanType</remote> <ejb-link>Fred</ejb-link> </ejb-ref> − Both local and remote references are possible. © 2007 Will Provost. 9 All Rights Reserved by Capstone Courseware, LLC.
  • 10. Using Stateful Session Beans • The above technique is actually appropriate only for stateless session beans, because a client needs only one reference to a stateless bean, even if it will make many calls to that bean. • Stateful beans won’t work this way. − Whatever injection method we choose, we will still have one reference to one bean. − How can we wedge all our different client conversations – our own sessions – into that one object? It will surely become confused! • For a stateful bean, the general technique is to use the servlet context to look up the stateful bean each time you need it: ((ServletContext) FacesContext.getCurrentInstance () .getExternalContext ().getContext ()) .lookup ("thatStatefulBean"); − This requires an <ejb-local-ref> or <ejb-ref> just as used in ordinary injection by name. − This will trigger creation of a distinct bean for your session; typically this reference would be saved at session scope in the web application as well. • In our upcoming example, we use a stateless bean that acts as a factory for stateful beans. − This simplifies life for the web application, as it can use a simple @EJB annotation and then call the factory method as needed. © 2007 Will Provost. 10 All Rights Reserved by Capstone Courseware, LLC.
  • 11. Tools and Environment • For our practical exercises with EJB, we’ll rely on the Java EE 5.0 SDK from Sun Microsystems. • The Java EE 5.0 SDK must be installed on your system, typically in c:SunSDK. • Other tools are deployed with the example code, under c:/Capstone/Tools: − The JSTL libraries are in JSTL1.1. − MySQL and its JDBC driver are in MySQL5.0. • Be sure that the following environmental settings are correct: − The executable path includes the bin and jdk/bin directories under the SDK installation root. − The environment variable JAVA_HOME is set to the jdk directory. − The environment variable JAVA_EE_HOME is set to the SDK installation root itself. − An environment variable CC_MODULE is set to c:CapstoneJSF+EJB. • Copy the file mysql-connector-java-5.0.4-bin.jar from c:/Capstone/Tools/MySQL5.0/lib to %JAVA_EE_HOME%/lib. − This will assure that the Java EE server can find the MySQL JDBC driver on the server’s own classpath. © 2007 Will Provost. 11 All Rights Reserved by Capstone Courseware, LLC.
  • 12. Starting the MySQL and Java EE Servers • To start the MySQL RDBMS, simply run the following command from c:/Capstone/Tools/MySQL5.0/bin: mysqld − Note that the console you use to run this command will appear to hang; this is the MySQL daemon process listening for connections at its default port 3306. • When you want to shut the server down again, run this prepared script from the same directory: shutdown • Start the application server from the Windows Start menu, as shown below ... − You’ll see confirmation that the server is running and ready for requests, and a prompt to hit any key – do so. • You can stop the server from the menu – but leave it running for now. © 2007 Will Provost. 12 All Rights Reserved by Capstone Courseware, LLC.
  • 13. The LandUse Application EXAMPLE • In c:/Capstone/JSF+EJB/Examples/LandUse, there is a version of the LandUse application in which a JSF GUI uses session and entity beans by way of dependency injection to a managed bean. • In this presentation we will focus on the bridge between JSF and EJB, and see a summary of their interactions. • We leave details of the rest of the application for further study. − The JSF code is almost identical to that found in the JSF course. − The same can be said of the EJBs and relational database as found in the EJB course. • The bridge between the web tier and EJB tiers of this application is found in the dependencies from a single managed bean to a stateless session bean and a stateful session bean: © 2007 Will Provost. 13 All Rights Reserved by Capstone Courseware, LLC.
  • 14. The LandUse Application EXAMPLE • First, consider the JSF configuration file, found in docroot/WEB-INF/faces-config.xml: <managed-bean> <managed-bean-name>DB</managed-bean-name> <managed-bean-class> gov.usda.usfs.landuse.web.DatabaseWrapper </managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> − The managed bean DB is the gateway to the business logic and persistent data for the application. − In the JSF-only version of this application, this bean manages the loading and saving of a file of serialized objects. − Here, it will connect to EJBs. © 2007 Will Provost. 14 All Rights Reserved by Capstone Courseware, LLC.
  • 15. The LandUse Application EXAMPLE • In src/gov/usda/usfs/landuse/web/DatabaseWrapper.java, see the simple injection-by-type use of the @EJB annotation: @EJB private LandUseService landUseService; • This session-scope bean uses the stateless bean to get “global” information such as the list of proposals in the database. private String loadData () { List<Proposal> proposals = landUseService.getAll (); ... } − And how are we assured that this method is invoked when the bean is created? − The Java-EE annotation @PostConstruct assures that the init method will be called; this calls loadData. @PostConstruct public void init () { loadData (); } − This method is akin to a constructor, but will be called only after injected dependencies have been set. © 2007 Will Provost. 15 All Rights Reserved by Capstone Courseware, LLC.
  • 16. The LandUse Application EXAMPLE • Then, when the user begins working with a specific proposal, the managed bean derives a reference to the stateful session bean – see the edit method: public String edit () { proposalService = null; for (int i = 0; i < data.size (); ++i) if (data.get (i).isSelected ()) { proposalService = landUseService.getServiceForProposal (data.get (i).getProposal ().getID ()); selectedProposal = proposalService.get (); return "detail"; } ... − When the Done or Decide button is clicked, the managed bean throws away its reference to the stateful bean with a call to the helper method clearDetailState, and returns to the summary page. private void clearDetailState () { proposalService = null; possibleDecision = new Decision (); possibleComment = new Comment (); } © 2007 Will Provost. 16 All Rights Reserved by Capstone Courseware, LLC.
  • 17. The LandUse Application EXAMPLE • Set up, build, and deploy the application as follows: 1. If you’ve deployed any of the LandUse steps from the EJB course, clean them up first: asant clean-DB 2. Create the underlying database of land-use proposals, and establish JDBC resources for the application server: asant init-DB 3. Build the application and prime the database using a simple Java SE console application: asant build run PrimeWithData 4. Deploy the EAR file that holds the web and EJB tiers of the application: asant deploy • The full console output from this series of commands (starting with asant init-DB) can be found in ConsoleLog.txt. © 2007 Will Provost. 17 All Rights Reserved by Capstone Courseware, LLC.
  • 18. The LandUse Application EXAMPLE • Open the following URL in your web browser: http://localhost:8080/LandUse − To produce this page, the JSF component tree derives data from the managed bean – which in turn calls getAll on the stateless LandUseServiceImpl. © 2007 Will Provost. 18 All Rights Reserved by Capstone Courseware, LLC.
  • 19. The LandUse Application EXAMPLE • Choose one of the proposals, and click Edit: − This is where the stateful bean is brought into play, and from this point forward the (session-scope) managed bean will use the same stateful bean for its operations: adding comments, changing basic data, or rendering decision. © 2007 Will Provost. 19 All Rights Reserved by Capstone Courseware, LLC.
  • 20. Impact on the JSF Application • A few significant changes were required on the JSF side to work with EJBs instead of a private file of serialized objects. − In fact most of these have more to do with moving to a multi-tier architecture than with EJB specifically; the EJB impacts are really no more and no less than those mentioned in the example. • State management in the web tier is a little different. − The JSF-only application tracks the selected proposal for a user session as an ordinal value into a List. − This is fine when (a) we have the list to ourselves so it’s not volatile, and (b) ordinal values are meaningful! − For a database, ordinal values are usually unreliable, and instead it’s an ID or primary key that matters. − So the managed bean tracks its selectedProposal as an object reference, along with the stateful-bean reference proposalService. • One simple but critical change that is specific to Java EE: the web.xml must declare that the application uses Servlets 2.5. − JSF can be configured for a version-2.4 application, but dependency injection is a 2.5 feature. − Change the version back to 2.4, and watch the connection to the stateless session bean quietly fail to materialize. © 2007 Will Provost. 20 All Rights Reserved by Capstone Courseware, LLC.
  • 21. JPA Annotations • With a full Java-EE library on hand – but even without an EJB container – Java code can take direct advantage of the JPA. − Thus EJBs are not essential to JPA persistence, but rather one means of implementing that persistence. • Implementing a persistent type using JPA involves a wide range of annotations beginning with @Entity and also including definitions of table and column mappings, association cardinality, and eager vs. lazy fetching strategies. − This is beyond our scope. • To use a JPA entity class to carry out database operations is a simpler matter: − The entry point is an object of type EntityManager. − It represents an abstraction known as the persistence context. − A persistence context is an instance of a persistence unit. • A persistence.xml file will define one or more persistence units. − For EJB JARs this is found in the JAR’s META-INF directory. − For standalone applications it is found in a META-INF directory anywhere off the class path. − For web applications it goes in the WEB-INF directory. • The JPA implementation will find this definition and seek out a context when the application attempts to create an EntityManager. © 2007 Will Provost. 21 All Rights Reserved by Capstone Courseware, LLC.
  • 22. Using JPA from Web Components • When EJBs use JPA entities, they typically adopt a fully declarative strategy by which the EJB container manages transaction boundaries and persistence contexts. − Transaction and context will live for a single method call. − The bean can declare different requirements, such as no transactions or a context that lives longer than a method call. • For web components, things are different: − Web containers will not manage transactions. − There is no analog to the per-method scope for persistence contexts; and we often need longer durations such as for the life of a user session. • Therefore a JSF application adopts a different strategy – partly declarative and using dependency injection, but governing transaction and persistence-context boundaries manually. − Declare an injectable EntityManagerFactory using the @PersistenceUnit annotation. − Create and close EntityManagers as needed. − A sound approach uses lifecycle methods on a managed bean, identified by @PostConstruct and @PreDestroy annotations. − Control transaction boundaries using the EntityTransaction interface, an instance of which is exposed by EntityManager. © 2007 Will Provost. 22 All Rights Reserved by Capstone Courseware, LLC.
  • 23. The Human Resources Application EXAMPLE • In c:/Capstone/JSF+EJB/Examples/HR there is a simple JSF application that shows department and employee records from a human-resources database, and allows limited data editing. • A single session-scope bean of type DatabaseWrapper invokes persistence methods to manipulate JPA entities for Department and Employee records: © 2007 Will Provost. 23 All Rights Reserved by Capstone Courseware, LLC.
  • 24. The Human Resources Application EXAMPLE • See src/cc/hr/web/DatabaseWrapper.java: this class coordinates all JPA activities for the application. − It defines an injectable EntityManagerFactory and also an EntityManager that it will manage by itself: @PersistenceUnit private EntityManagerFactory factory; private EntityManager mgr; − It controls the creation and closing of the EntityManager in its lifecycle methods; so we know that the persistence context will be in force for the user session: @PostConstruct public void createEntityManager() { mgr = factory.createEntityManager(); } @PreDestroy public void closeEntityManager() { mgr.close(); } © 2007 Will Provost. 24 All Rights Reserved by Capstone Courseware, LLC.
  • 25. The Human Resources Application EXAMPLE − It uses the EntityManager for queries and updates, wrapping the updates in an EntityTransaction: public List<Department> getDepartments () { data = (List<Department>) mgr.createQuery ("select x from Department x").getResultList (); return data; } public void update () { EntityTransaction tx = mgr.getTransaction (); for (Department department : data) try { tx.begin (); mgr.merge (department); tx.commit (); } catch (Exception ex) { System.out.println ("Exception during " + "transaction -- rolling back."); ex.printStackTrace (); try { tx.rollback (); } catch (Exception ex2) {} } } © 2007 Will Provost. 25 All Rights Reserved by Capstone Courseware, LLC.
  • 26. The Human Resources Application EXAMPLE • See docroot/WEB-INF/classes/META-INF/persistence.xml, which defines the JPA persistence unit for the application: <persistence-unit name="Earthlings" transaction-type="RESOURCE_LOCAL" > <provider> oracle.toplink.essentials.PersistenceProvider </provider> <jta-data-source> jdbc/Earthlings </jta-data-source> <properties> <property name="toplink.logging.level" value="INFO" /> </properties> </persistence-unit> • Set up, build, and test the application as follows. (MySQL and the Java EE application server should both still be running.) 1. Build the database: asant create-DB 2. Build and deploy the application: asant © 2007 Will Provost. 26 All Rights Reserved by Capstone Courseware, LLC.
  • 27. The Human Resources Application EXAMPLE 3. Test at the following URL: http://localhost:8080/HR 4. Try changing one or more employee salaries, and see that the department payroll accurately reflects the updated salaries. This is persistent now, so new sessions, redeploys of the application, etc., all will see the durable data from the MySQL database. © 2007 Will Provost. 27 All Rights Reserved by Capstone Courseware, LLC.