No-Interface Client View Example in EJB3.1

No-Interface Client View

  • The EJB 3.1 specification addresses no-interface local view.
  • The no-interface view has the same behaviour as the EJB 3.0 local view. However, a no-interface view does not require a separate interface, that is, all public methods of the bean class are automatically exposed to the caller.
  • By default, any session bean that has an empty implements clause and does not define any other local or remote client views, exposes a no-interface client view.
  • For example, the following session bean exposes a no-interface view:

Environment Used

  • JDK 6 (Java SE 6)
  • EJB 3.1
  • Web Client – Servlet 2.5 API
  • Eclipse Indigo IDE for Java EE Developers (3.7.1)
  • JBoss Tools – Core 3.3.0 M5 for Eclipse Indigo (3.7.1)
  • JBoss Application Server (AS) 7.1.0 Final

Setting up development environment:
Read this page for installing and setting up the environment for developing and deploying EJB 3.x on JBoss application server.

Project Description

  • We are going to create a simple EJB 3.1 No-Interface client view example for a Session Bean and a Web client (Servlet) which access the business methods of bean.
  • The session bean and the Servlet are deployed on the same server instance (JBoss AS).

Creating New EJB Project

  • Open Eclipse IDE and create a new EJB project which can be done in three ways,
    • Right click on Project Explorer -> New -> EJB Project
    • File menu -> New -> EJB Project
    • Click on the down arrow on New icon on toolbar -> EJB Project

  • Enter the project name as “EJB3.1NoInterfaceView” and make sure the JBoss 7.1 Runtime has been selected with the EJB 3.1 Module version.
  • Click Next -> Next -> and Finish.

Creating Stateless Session Bean

  • Right click on ejbModule -> New -> Session Bean (EJB 3.x)
  • Enter the Java package name as com.theopentutorials.businesslogic
  • Enter the Class name as FirstBean
  • Select the State type as Stateless
  • Select No-Interface check box
  • Click “Finish

Enter the following code in bean class:

package com.theopentutorials.businesslogic;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

@Stateless
@LocalBean
public class FirstBean {
    public FirstBean() {
    }

    public String sayHello() {
		return "Hello";
    }
}

The client of a no-interface view always acquires an EJB reference — either through injection or JNDI lookup. The only difference is that the Java type of the EJB reference is the bean class type rather than the type of a local interface. This is shown in the following bean client:

Creating Servlet Client (Dynamic Web Project)

  • The next step is to write a Web Client (a Servlet) which invokes the methods of bean.
  • The servlet uses Dependency Injection (DI) to get the bean reference.
  • Creating a new ‘Dynamic Web project’ can be done in three ways,
    • Right click on Project Explorer -> New -> Dynamic Web Project
    • File menu -> New -> Dynamic Web Project
    • Click on the down arrow on New icon on toolbar -> Dynamic Web Project

  • Enter the project name as “EJB3.1NoInterfaceClient” and make sure the JBoss 7.1 Runtime has been selected with the Dynamic web module version as 2.5.
  • This project uses Java 1.6 version. The default Java version in Eclipse Indigo is 1.7 so to change the “Configuration” click on “Modify…” button to make changes.
  • Click “Next” -> “Next” -> and “Finish”.
  • You will see the Dynamic web project in the “Project Explorer” view.

Since the Servlet is created in a separate project it needs EJB JAR file to do Dependency Injection.

Creating JAR file

  • Right click on FirstBean.java file in EJB3.1NoInterfaceView -> Export.
  • Expand Java folder and select “JAR file” and click Next.
  • Click on Browse… and enter the file name and Finish. In our example, the file name is given as EJB3NoInterface

Now copy the created JAR file and paste it in EJB3.1NoInterfaceClient/WebContent/WEB-INF/lib folder.

Creating Servlet class

  • Right click on src or Project -> New -> Servlet

  • Enter the Java package name as com.theopentutorials.servlets
  • Enter the Class name as EJB31ServletClient
  • Click “Finish

In the doGet() method copy the following code.

package com.theopentutorials.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibytecode.businesslogic.FirstBean;

public class EJB31ServletClient extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	FirstBean bean;

	public EJB31ServletClient() {
        	super();
       	}

	protected void doGet(HttpServletRequest request, 
			HttpServletResponse response) 
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println(bean.sayHello());  
	}
}

Deploying the dynamic web project

  • Now we need to deploy the Servlet project on server.
  • Right click on “JBoss 7.1 Runtime Server” available in Servers view -> Add and Remove… -> Select the Servlet WAR file from the left pane and click Add-> and then Finish.

Start/Restart the Server

  • Right click on “JBoss 7.1 Runtime Server” from Servers view and click on Start if it has not yet been started.

You will get the following output in the browser

Hello

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.