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

Updated:   |  IBM WebSphere articles

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 java.io.IOException;
import java.io.PrintWriter;
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.

Notice that the EJB frontend application makes a connection to the EBJ backend using iiop:<hostname>:<ORB port>. In the WebSphere admin console, navigate to Servers > All servers > the application server running the EBJ backend application > Ports and the ORB port will be displayed (9100 by default). Also, If there is a firewall between the frontend and backend, ensure connections on the ORB port are allowed in the firewall.

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

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();

  Properties props = new Properties();
  props.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
  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"
    out.print(bean.sayHello());     	            
    } catch (NamingException e) {
      e.printStackTrace();
    }
  }

 

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.

<web-ext
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://websphere.ibm.com/xml/ns/javaee"
  xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_0.xsd"
  version="1.0">
  <context-root uri="/Frontend"/>
</web-ext>

 

You can now export the EJB Frontend application into a WAR, and deploy the WAR to one of your WebSphere application servers. Then, configure the application server that contains the frontend WAR 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.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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