This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Using servlet to call EJB and give the response. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Using servlet to call EJB and give the response." Watch "Using servlet to call EJB and give the response." New topic

Using servlet to call EJB and give the response.

Arjun Karthick
Ranch Hand

Joined: Jan 04, 2007
Posts: 77
Hi all,
I have an EJB application which is exposed as a service to other stand alone services. I have developed that EJB application in the Weblogic container. i have tested the EJB application using a standalone code. Now I have to use a web application that calls the EJB application and displays the result in the JSP.
I have planned to convert the code in the stand alone code to a servlet. Is this approach correct. If correct how do I get assess to various util classes that the standalone code uses.

What are the code change I have to do to my standalone code to act as a servlet?

Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Hi Arjun,

You can convert your code to use Servelt so as to invoke an EJB.

props = new Properties(); props.put(Context.PROVIDER_URL, "t3://localhost:7001");props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");ctx = new InitialContext(props);obj = ctx.lookup("TestApp");home = (TestHome)javax.rmi.PortableRemoteObject.narrow(obj, TestHome.class);

Have this code in the init() method of the servlet and store the home object in a member variable in the Servlet. [You should have that code in the init method which you want to initialise once and that can be reused by all the clients, please read Servlet specs for this]

Have all other code in your service method.

Now you compile the Servlet and bundle it in a war file. Have all the classes(util classes, stubs, home and remote interface of EJBs) in the WEB-INF/classes folder and all the jars in the WEB-INF/lib of the war. Have proper servlet mapping in the web.xml for the Servlet.

Hope this is what you are looking for in the Servelt.

Amit Tank

Amit Tank
Linked In
Arjun Karthick
Ranch Hand

Joined: Jan 04, 2007
Posts: 77
Hi Amit,
Thanks for the guidence.
But again I have a doubt. The Ejb Home, remote and stub are part of that EJB application. How can i get the reference of those object. Should i do that by importing that is by adding that jar in the lib folder or by using the web.xml file i can give the reference to the EJB. Please advice on the same.

Abhilash George

Joined: Apr 28, 2006
Posts: 13
yes you need to have the client jar file for the created EJBs . At the same time you need to create the reference so as to create an alias so that you can use a local reference for the global jndi so that your code remains safe from changes in global jndi name

I agree. Here's the link:
subject: Using servlet to call EJB and give the response.
It's not a secret anymore!