Stateless Bean based on Remote Interface

04 Mar

package com.xavier.ejb3.sessionbean;

public interface HelloEJBRemote {

        public String hello(String name);


package com.xavier.ejb3.sessionbean;

import javax.ejb.Remote;

import javax.ejb.Stateless;




public class HelloEJBRemoteBean implements HelloEJBRemote {

    public String hello(String name) {

return new String(“Hello, “ + name);





1) If you want to rename the Session Bean’s name – @Stateless(name=”HelloEJB”), if no name parameter, then the Session Bean would be class name.

2) If EJB Bean implements multiple business interfaces: @Remote({HelloEJBRemote.class, UserEJBRemote.class});



<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>

<%@ page

import=“javax.naming.*, com.xavier.ejb3.sessionbean.*, com.xavier.ejb3.entitybean.*, java.util.*”%>

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>



<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>

<title>Hello EJB Remote Bean</title>





final Hashtable jndiProperties = new Hashtable();

jndiProperties.put(Context.PROVIDER_URL, “localhost:1099”);

jndiProperties.put(Context.URL_PKG_PREFIXES, “org.jboss.ejb.client.naming”);

Context ctx = new InitialContext(jndiProperties);

HelloEJBRemote hr = (HelloEJBRemote)ctx.lookup(ejb:ejb3/ejb3//HelloEJBRemoteBean!com.xavier.ejb3.sessionbean.HelloEJBRemote);

out.println(“<br>Invoke EJB via Remote Interface<br>” + hr.hello(“Xavier”));           

}catch(Exception e){

out.println(“<br>Invoke Remote Interface failed.”);






1) JBoss AS 7.1 EJB Session Beans’ naming convention rules:

For stateless beans:


For stateful beans:


2) JNDI Properties.

Here we are creating a JNDI InitialContext object by passing it some JNDI properties. The Context.URL_PKG_PREFIXES is set to org.jboss.ejb.client.naming. This is necessary because we should let the JNDI API know what handles the ejb: namespace that we use in our JNDI names for lookup. The “org.jboss.ejb.client.naming” has a URLContextFactory implementation which will be used by the JNDI APIs to parse and return an object for ejb: namespace lookups. You can either pass these properties to the constructor of the InitialContext class or have a jndi.properites file in the classpath of the client application, which (atleast) contains the following property:


Leave a comment

Posted by on 03/04/2012 in EJB


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: