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?
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.
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.
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