Two Laptop Bag*
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

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?



Regards,
Arjun.
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.

Cheers,
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.

Regards,
Arjun.
Abhilash George
Greenhorn

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


george
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using servlet to call EJB and give the response.
 
Similar Threads
EJB 3.0 Problems
NoInitialContextException
Error----web app could not find ejb module.
Using servlet to call EJB and give the response.
Accessing EJB from a Swing Client