16.10.2015 Views

Getting Started with WebSphere Application Server

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

94 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>WebSphere</strong> <strong>Application</strong> <strong>Server</strong> Community Edition<br />

public class TestServlet extends HttpServlet {<br />

...<br />

@EJB(name=”HelloEJB”)<br />

private HelloInterfaceLocal helloejb;<br />

protected void doGet(HttpServletRequest request,<br />

HttpServletResponse response) throws<br />

ServletException, IOException {<br />

response.getWriter().println(ejb.sayHello());<br />

}<br />

...<br />

}<br />

Listing 5.6 - Using the @EJB annotation<br />

With @EJB, helloejb will reference to an EJB named HelloEJB. If there is only one<br />

implementation for HelloInterfaceLocal, the name value can be omitted in the @EJB<br />

annotation.<br />

5.3.1.2 Add the EJB into the dependency list of the servlet<br />

As mentioned earlier, the deployment plan of an EJB project is called openejb-jar.xml<br />

and all dependencies should be declared there. If you want to reference an EJB, add it as<br />

a dependency in the deployment plan. Listing 5.7 provides an example.<br />

...<br />

<br />

<br />

default<br />

TestWeb<br />

1.0<br />

car<br />

<br />

<br />

<br />

default<br />

TestEjb<br />

1.0<br />

car<br />

<br />

<br />

<br />

...<br />

Listing 5.7 - Adding an EJB as a dependency in the deployment plan<br />

As you can see, the dependency declaration here is similar to the EJB definition in<br />

openejb-jar.xml of Listing 5.5.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!