JEE with Eclipse WTP

p.(excerpt) This article explain how to setup a JEE Project on your application server using Eclipse WTP.

  1. JEE with Eclipse WTP
    1. EJB Project
    2. Dynamic Web Project
  2. References

Prerequisites: install Eclipse WTP, and setup an application server to run JEE project on appropriate target runtime.

EJB Project

In workspace create New > EJB Project; give it a simple name “HelloEJB”, check Target runtime is set to your application server, and click on Finish.

In the project create New > EJB 3 Session Bean; define the package org.greetings, give it the name Hello, and clicK on Finish.

Note that the operation has created a remote interface named Hello, and an implementing class named HelloBean

In the Hello interface define the business method sayHello

public interface Hello {
    public String sayHello(String name);

In the HelloBean write the implementation.

public @Stateless class HelloBean implements Hello { 
    public String sayHello(String name) {
        return "Hello, " + name + "!";

Dynamic Web Project

The next step is to create a Web project that contain our presentation layer

Now, in the workspace create a New > Dynamic Web Project; simply call it Hello and click on Finish.

Inside the Hello project create a New > Servlet; set org.greetings as package name and HelloServlet as class name, then click Next.

In the second page give it a short name hello (note that the mapping is automatically changed in /hello) then click Next.

In the last page deselect the doPost method, so we will have only the doGet, then click Finish.

I need for my presentation logic the reference to the Bean that contain the business logic, os i add a compile-time reference to my EJB project.

from Hello project > properties > Java Build Path select the Properties tab; click Add, add a reference to HelloEJB then click Ok and close.

Now I can open the hello servlet; insert a reference to local business interface of the bean; and, by using the @EJB annotation, I inject the HelloBean into the hello variable.

Then I can implement the doGet method in which i retrieve the name parameter from httpRequest, and I can pass it to the HelloBean, and, agian, i pass the result as response of the Hello servlet.

See the code

private Hello hello;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name = request.getParameter("name");
    String out = hello.sayHello(name);
    response.getOutputStream().println( out );