FreeKB - Creating your first WebSphere EJB (Enterprise Java Bean) app - Frontend WAR
Creating your first WebSphere EJB (Enterprise Java Bean) app - Frontend WAR

Home > Search

This assumes you have already created the backend EJB and deployed the JAR to WebSphere from this article.


Create Dynamic Web Project

In Eclipse, select File New > Dynamic Web Project. Give your project a name, such as "EJB Frontend", select WebSphere or Liberty server runtime, and select Finish. I also like to remove the check mark from "add to EAR", just to reduce the cluter when learning something now. EJB Frontend should be added to the left panel of Eclipse.


Java Build Path

Technically, this is not necessary, but really helps to elminate red X from appearing in the servlet. Select Project > Properties > Java Build Path > Add External JARs, and add the EJB Backend JAR.


Create Package

Under the src folder, create a new package (ejb.client in this example).


Create Servlet

Right-click on the ejb.client package, and select New > Servlet. Give the new servlet a name, such as EJBServlet. Import the following packages in the servlet.

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ejb.remoteinterface.Greeting;


Add the following markup in the doGet method.

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
	        Properties props = new Properties();
	        props.put(Context.INITIAL_CONTEXT_FACTORY, "");
	        props.put(javax.naming.Context.PROVIDER_URL, "iiop://<hostname of your WebSphere application server>:<ORB port of your WebSphere JVM>");
	        Greeting bean = null;
	        Object obj;
	        try {
	            InitialContext ctx = new InitialContext(props);

                // HelloJNDI is what we configured in our server side JAR in WebSphere
	            obj= ctx.lookup("HelloJNDI");
	            if (obj instanceof Greeting) {
	                bean = (Greeting) obj;

                // This will print "Hello World"
	        } catch (NamingException e) {


Under WebContent, right click on the WEB-INF folder and select New > File. Name the file ibm-web-ext.xml.


Right click on ibm-web-ext.xml, and select Open with > XML Editor. Add the following markup. This tells WebSphere that the context root of the servlet will be /Frontend.

  <context-root uri="/Frontend"/>


You can now export the EJB Frontend application into a WAR, and deploy the WAR to the same WebSphere application server that is running the EJB Backend JAR. You will also need to configure the JVM or application to use the Backend JAR, so that the Frontend application can interact with the classes in the Backend JAR file. After the JVM has been configured to use the Backend JAR, when requesting the EJB Frontend servlet in the browser, the servlet will make a call to the Backend JAR, and return whatever is contained in the class being called. In this example, "Hello World" is being produced from the Backend JAR to the Frontend servlet.

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.