Now all we need is a client...

We have a freshly deployed bean (in a J2EE server), but we can’t test it until we have a client. The client has to do five things:

  1. Get a reference to a JNDI InitialContext (we’ll learn about that in the Client View chapter).

  2. Use the InitialContext to do a lookup on the home interface of the bean (that we named “Advisor” when we deployed).

  3. Narrow and cast the thing we get back from the lookup. (That thing is something that implements the AdviceHome interface.) We’ll learn about narrowing in the Client View chapter.

  4. Call create on the home interface to get back a reference to the component interface.

  5. Call getAdvice() (the business method, the reason we’re here, remember?) on the component interface, and print the result.

Note

We’re using a stand-alone Java program as the client.

In the real world, your clients will likely be servlets or other beans.

The five things the client must do are the same regardless of the type of client.

So, using a stand-alone Java program teaches you to do the same things you’ll need to do with a servlet client.

If your client is another enterprise bean, the code will be slightly different, but you still have to do the five steps.

Relax

There’s nothing on the exam about servlets or JSPs.

The exam expects you to know how the client gets and uses a bean (the steps above), but the type of client doesn’t matter. The client code for getting a reference to a bean, and ultimately calling methods on the bean, is virtually the same whether the client is a servlet or stand-alone Java app. And all the exam cares about is the part of the code where the client is trying to get and use a bean.

The only knowledge of servlets and JSPs you need for the exam, is to know that the EJB spec does NOT guarantee support for them. Servlets and JSPs are guaranteed by the J2EE spec, but not by the EJB spec.

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.