Java EE 6

No, Java EE is not J2EE ! Java EE has suffered from its previous versions and some words have become evil. EJB for example. No matter how much easier EJB 3.1 is, it's still called an EJB and that has some bad memories because we remember Home/Remote interfaces, Bean classes with empty methods, XML files and JNDI lookups with narrowing, difficult to test… Another word issue is the Entity Bean. We all remember the Entity Bean 2.1 nightmare (heavyweight and overly complicated). JPA 2.0 has simplified the model and brings a huge power with it. JAX-RPC vs JAX-WS… and so on.

Compared to J2EE 1.4, Java EE 6 has a completely different flavor : reduced complexity, less XML file descriptors, less code, injection, coding by exception… Despite all these positive aspects, Java EE projects are not taking off. This is where Jolorun comes in the play; to show you how easy it is to develop an application with Java EE 6.

Don't believe it ? Well, nothing is better than a bit of code to convince you. Here's the code for a servlet that calls an EJB stateless that persists a Customer object into a database.

import javax.servlet.http.annotation.*;
import javax.ejb.*;

@Servlet(urlMappings={"/createcustomer"})
public class CustomerServlet {

    @EJB
    private CustomerService customerService;

    @GET
    public void handleGet(HttpServletRequest req, HttpServletResponse res) {
        Customer c = new Customer();
        c.setFirstname("Olodumare"); 
        c.setLastname("Olorun"); 
        c.setTelephone("666 777 888"); 
        c.setEmail("exu@yoruba.org"); 
        customerService.createCustomer(c);
    }
}

import javax.ejb.*;
import javax.persistence.*;

@Stateless
public class CustomerService  {

    @PersistenceContext(unitName = "jolorunPU")
    private EntityManager em;

    public Customer createCustomer(Customer customer) {
        em.persist(customer);
        return customer;
    }
}

import javax.persistence.*;

@Entity
public class Customer {

    @Id
    @GeneratedValue
    private Long id;
    private String firstname;
    private String lastname;
    private String telephone;
    private String email;

    public Customer() {
    }

    public Long getId() {return id;}

    public String getFirstname() {return firstname;}
    public void setFirstname(String firstname) {
      this.firstname = firstname;
    }

    public String getLastname() {return lastname;}
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getTelephone() {return telephone;}
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getEmail() {return email;}

    public void setEmail(String email) {
        this.email = email;
    }
}

No local interface for the EJB, no ejb-jar.xml, no web.xml, no application.xml, no database mapping (just a persistence.xml file)… This same simplicity is used with Web Services, RESTful services, JSF and so on. Give it a try, ask Jolorun to create an application for you, customize it the way you want, check the code and make up your own mind. Java EE 6 is definitely the right platform if you want to develop a web or enterprise application.

TODO : unit testing class

Comments 0

No comments for this document
Java Champion

Paris JUG Leader

Cast Codeurs

Antonio Goncalves' personal website