Bonjour, comment Java?

I'm preparing some Java EE 6 JavaOne demos. While doing that, I was thinking: how can I compress most of the Java EE 6 technology inside *one* single Java Class?
If you are my manager, stop reading now...
If you believe you are an architect, stop reading now...
If you are a member of the Java Blueprints team, stop reading now...
If you are a regular java blog reader, stop reading now...
The following code is PG 40. Talk to you parents if you are less than 40.

Still reading? Not sure why, but here we go.

The following Java EE 6 compliant Application is a Web Application with just 2 source files: and persistence.xml.
It demonstrates the following Java EE 6 features:

  • Annotated Servlets 3.0, avoiding the need for web.xml!!!
  • EJB without local Interface: 100% POJO, and transactional (managed by the container) and securable
  • EJB inside a Web Application
  • JPA (still needs a persistence.xml ,  here it's defined as creating the tables at deploy time, assuming the database is already running)
  • EclipseLink Implementation
  • Default GlassFish v3 JavaDB database registered as "jdbc/__default" datasource.
  • Beans Validation framework (JSR 303...Merci Emmanuel)
  • Injection of EJBs inside a Servlet

Are you still reading? Remember the 3 tier architecture, with Data Layer, Business Layer, then Presentation Layer? Well forget it for this monster application which merges all the layers into one single Java Class, which is a Servlet, an EJB and a Entity JPA bean validated with some Bean Validation annotations...
package monster;

import java.util.*;
import javax.ejb.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.persistence.*;
import javax.servlet.*;
import javax.validation.*;
import javax.validation.constraints.*;
* @author: Not Me!!!
@Stateless @Entity @WebServlet(urlPatterns = "/monster")
@Table(name = "MONSTERTABLE")
@NamedQueries({@NamedQuery(name = "MonsterEJB.findAll", query = "SELECT c FROM MonsterServlet c")})
@PersistenceContext(name = "monsterContext", unitName = "MonsterWebAppPU")
public class MonsterServlet extends HttpServlet {
    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private int monsterId;
    @Max(2) @NotNull private String name;
    @Transient @EJB MonsterServlet monsterEJB;
  /*@Transient @PersistenceUnit(unitName = "MonsterWebAppPU") //does not work see EclipeLink bug
   private EntityManagerFactory emf;*/ //
   public void doGet(HttpServletRequest request, HttpServletResponse response)
ServletException, IOException {
       response.getWriter().println("In Servlet calling the EJB side " + monsterEJB.EJBBusinessMethod("" + this));

    public String EJBBusinessMethod(String name) {
  try {
        InitialContext ic = new InitialContext();
        EntityManager em = (EntityManager) ic.lookup("java:comp/env/monsterContext"); = name;


        Query allMonsterQuery = em.createNamedQuery("MonsterEJB.findAll");
        List allMonsters = allMonsterQuery.getResultList();
        String error = validateTheMonsterJPA((MonsterServlet) allMonsters.get(0));
      return "BusinessMethod from EJB" + allMonsters.toString() + error;
    } catch (NamingException ex) {
     return "Error in EJBBusinessMethod "+ex.getMessage();

    private String validateTheMonsterJPA(MonsterServlet m) {
       String error=" ";
       ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
       Validator validator = factory.getValidator();
       Set<ConstraintViolation<MonsterServlet>> s = validator.validate(m);
     for (ConstraintViolation<MonsterServlet> c : s) {
            error = error+ " Contraint Violation: " + c.getMessage());
    return error;

And the persistence.xml to put in the WEB-INF/classes/META-INF directory of this Web Application

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns=""
    <persistence-unit name="MonsterWebAppPU" transaction-type="JTA">
  <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

This Web Application can be deployed as it to the latest builds of GlassFish v3. The page output is something like:

In Servlet calling the EJB side BusinessMethod from EJB[monster.MonsterServlet@5718c3a7]  Contraint Violation: must be less than or equal to 2

Now the quiz:

  1. Explain the following statement: @Stateless @Entity @WebServlet
  2. what do you need to change in the code to remove the Contraint Violation statement in the output?
  3. Why do we need to inject the EJB aspect of this class into the servlet and cannot  just use "this" ?
  4. Do you think this bug will be fixed? 

Still reading? Now I have just corrupted your brain: while not recommended, it is possible to write a 3-tier Java EE 6 Application with Servlets, EJBs, JPAs, Beans Validation in one single Java class and one single xml file...
Can't wait for Java EE 7 when persistence.xml will become optional:-)

See you at JavaOne. You can now forget this blog. Someone had to write it. It's over now:-)