JavaEE5NetBeans by manimoney707


More Info
									Java EE 5 Overview

Sang Shin Technology Architect Sun Microsystems

• • • • • • Primary goal Java EE 5 Web Services 2.0 EJB 3.0 New Java Persistence API (JPA) JavaServer Faces Q/A Focus of this presentation is NetBeans support of Java EE 5 Features (not the features themselves)!

You can try all the demos yourself by doing the handson lab!


Java EE 5 Goal

Make it easier to develop Java EE applications
Especially when first getting started with Java EE

How did we make it easier?
Then • Deployment descriptors • Required container interfaces • JNDI Lookups • Configuration files and command line options • No Supported UI Framework Now Java language annotations Plain Old Java Objects (POJOs) Dependency Injection More and better defaults Java Server Faces (JSF)

Java EE 5 Major Features
• • • • • Simplified web services support Greatly simplified EJB development New Java Persistence API (JPA) Dependency Injection Easy web application development with JavaServer Faces

• And fully compatible with J2EE 1.4

Annotations in Java EE 5
For defining and using web services To greatly simplify EJB development To map Java classes to databases (O/R mapping) To specify external dependencies (Dependency injection) • To map Java classes to XML • To reduce need for deployment descriptors • • • •

Java EE 5 Web Services

J2EE 1.4 Web Service (Old way)
package endpoint; import java.rmi.*; public class HelloServiceImpl implements HelloServiceSEI { public String sayHello(String param) throws java.rmi.RemoteException { return “Hello “ + param; } }
<?xml version='1.0' encoding='UTF-8' ?> <webservices xmlns='' version='1.1'> <webservice-description> <webservice-description-name> HelloService</webservice-description-name> <wsdl-file> WEB-INF/wsdl/HelloService.wsdl</wsdl-file> <jaxrpc-mapping-file> WEB-INF/HelloService-mapping.xml </jaxrpc-mapping-file> <port-component xmlns:wsdl-port_ns='urn:HelloService/wsdl'> <port-component-name>HelloService</port-component-name> <wsdl-port>wsdl-port_ns:HelloServiceSEIPort</wsdl-port> <service-endpoint-interface> endpoint.HelloServiceSEI</service-endpoint-interface> <service-impl-bean> <servlet-link>WSServlet_HelloService</servlet-link> </service-impl-bean> </port-component> </webservice-description> </webservices>

service implementation service interface

package endpoint; import java.rmi.*;

public interface HelloServiceSEI extends java.rmi.Remote { public String sayHello(String param) throws java.rmi.RemoteException; }

<?xml version='1.0' encoding='UTF-8' ?> <configuration xmlns=''> <service name='HelloService' targetNamespace='urn:HelloService/wsdl' typeNamespace='urn:HelloService/types' packageName='endpoint'> <interface name='endpoint.HelloServiceSEI' servantName='endpoint.HelloServiceImpl'> </interface> </service> </configuration>

Deployment descriptor

Java EE 5 Web Service (New way)
package endpoint; import javax.jws.WebService; @WebService public class Hello { public String sayHello(String name) { return “Hello “ + name; } }

DEMO: Java EE 5 Web Services
1. Make a POJO class Web Service with “@WebService” Annotation (Code-completion, Context-sensitive JavaDoc on Annotations) 2. Deploy the Web service and test it right within the IDE 3. Access WSDL document of the Web Service 4. Create and run Web service client •

EJB 3.0

EJB 3.0
• Dramatic simplification of all bean types • Regular Java classes (POJO) • Dependency injection

> @Stateless, @Stateful, @MessageDriven annotations > Use standard interface inheritance > Instead of using JNDI API to location components and

resources, let the container fetch them for you.

• Entity Beans (CMP) replaced with Java Persistence API (JPA) • Interceptors

Java EE 5 Stateless Session Bean
package endpoint; import javax.ejb.Stateless; @Stateless public class Hello { public String sayHello(String name) { return “Hello “ + name; } }

DEMO #1: EJB 3.0 Stateless Session Bean
1. Build a Stateless Session Bean using “@Stateless” annotation 2. Build Web application client that accesses the Stateless Session Bean e5basics/#Exercise_2

DEMO #2: EJB 3.0 Interceptors
1. Build an application that uses Interceptors


Java Persistence API (JPA)

Java Persistence API (JPA)
• Single persistence API for Java EE & Java SE • Developed by EJB expert group
and products (TopLink, Hibernate)

> Builds on years of experience with existing technologies

• Much simpler than EJB 2.x CMP • At least three implementations (all open source):
> Oracle – GlassFish/TopLink Essentials > JBoss – Hibernate > BEA – Kodo/OpenJPA

Persistence Unit
• Used to configure
> Persistence provider > Supports pluggability of persistence provider > Data source > Table Generation Setting > Create, Drop/Create, Nothing > Vendor specific properties

• The only XML configuration • NetBeans will create this for you

JPA – Persistence Unit

JPA – Object Relational Mapping
• Developer works with objects
> Database queries return objects > Object changes persist to the database

• Data transformation is handled by the persistence provider (TopLink, Hibernate, etc.) • Annotations define how to map objects to tables
> @Entity marks a regular Java class as an entity. > Class attributes map to table columns. Can be

customized with @Column. > Manage relationships: @OneToMany, ...

JPA – Entity Class
import javax.persistence.*; @Entity public class Person { @Id private String name; private int visits; public Person() { } public Person(String name) { = name; } public int incrementVisit() { return visits++; } }

JPA – Entity Manager
• EntityManager stores/retrieves data • Inject EntityManager: • Create an instance of the entity:
> Person p = new Person(param);

> @PersistenceContext private EntityManager em;

• Use EntityManager methods to persist data
> em.persist(p); em.merge(p); em.delete(p); > Person p = em.find(Person.class, param);

• Query using EJB QL or SQL

JPA – Entity Manager
... import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @WebService @Stateless public class Hello { @PersistenceContext private EntityManager em; public String sayHello(String param) { Person p = em.find(Person.class, param); if (p == null) { p = new Person(param); em.persist(p); } p.incrementVisist(); return “Hello “ + param + “: “ + } }

DEMO 1: Java Persistence API
1. Create Persistence Unit 2. Create Entity classes from Database tables 3. Use “TopLink” or “Hibernate” as Persistence Provider

DEMO 2: Java Persistence API
1. Use different strategy for Inheritance and see how database tables are created - SINGLE_TABLE - JOINED

JavaServer Faces 1.2

JavaServer Faces 1.2
• The Java EE Standard Web Application Framework
> Managed beans support dependency injection > Easy to use, powerful, extensible Expression Language,

shared with JSP

• Large market of JSF components

> Over 200 components from over 20 vendors, such as... > Apache, BusinessObjects, ESRI, Oracle, Sun, etc. > Including AJAX support >

• Java BluePrints AJAX Components

JavaServer Faces 1.2 in NetBeans
• Code Completion for:
> > > >

JavaServer Pages (JSP) JavaServer Faces (JSF), including expression language Java Standard Tag Libraries (JSTL) Any tab libraries added by the user

• Integrated documentation • HTML, JSP and JSF component palette

JavaServer Faces 1.2 in NetBeans
• Generation of JavaServer Faces applications from entity classes for CRUD operations
> Managed bean > Navigation rules

• • • •

Code is easy to read/change Helps to learn the patterns Gets you started quickly Code generation similar to Ruby on Rails...

DEMO: JavaServer Faces
1. Create JSF pages from Entity Classes Easily create CRUD (Create, Read, Update and Delete) database application 2. Create Managed Beans from Entity Classes

• The developer works less
language and XML
> No synchronization between Java programming

• The container works more

> Annotation processing, dependency injection > Less work to hide the complexity and XML descriptors > Does more useful things: wizards, hints, code validation,

• NetBeans 5.5 knows Java EE 5
code generation, etc.

• Java EE 5 - Did You Get Your Tools With That?

Questions & Answers

Java EE 5 – Did You Get Your Tools With That

Sang Shin Technology Architect Sun Microsystems

To top