Connecting Remote EJB3 from a Servlet with WebLogic 10.3
Right now i’m trying to connect 2 different applications that located in a different server. Im using remote EJB3 to connecting them, with Apache Tomcat as server 1 and WebLogic 10.3 as server 2. Im using Netbeans 6.8 as primary IDE for this test.
create an EJB Module Project from Netbeans IDE, and create these files
first is the remote interface
package com.edw.ejb3; import javax.ejb.Remote; @Remote public interface HelloEJBRemote { String sayHello(final String name); }
and the ejb implementation
package com.edw.ejb3; import javax.ejb.Stateless; @Stateless(mappedName="HelloEJB") public class HelloEJB implements HelloEJBRemote { public String sayHello(final String name) { return "Hello "+name+" how do you do?"; } }
Package it as JAR or EAR, start Weblogic then use Admin Console (default: http://localhost:7001/ ) to deploy the EJB.
and to make sure, you can check EJB’s JNDI
next is creating a Web Project, dont forget to include your EJB project to your Web Project’s library.
create a servlet file to perform a connection to Remote EJBs.
package ejb; import com.edw.ejb3.HelloEJBRemote; import java.io.IOException; import java.io.PrintWriter; import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class EJBServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); env.put(Context.PROVIDER_URL, "t3://localhost:7001"); Context ic = new InitialContext(env); HelloEJBRemote remote = (HelloEJBRemote) ic.lookup("HelloEJB#com.edw.ejb3.HelloEJBRemote"); out.println(remote.sayHello("Edwin")); } catch (Exception ex) { out.print(ex); ex.printStackTrace(); } finally { out.close(); } } }
and this is my web.xml configuration file
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>EJBServlet</servlet-name> <servlet-class>ejb.EJBServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>EJBServlet</servlet-name> <url-pattern>/ejb</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
if you find this error,
javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]
it happen because you havent put wlfullclient.jar (WebLogic library) into your Apache Tomcat’s library folder. You can find it in your Weblogic installation folder ({weblogic}\server\lib) or you can create it your self, by executing wljarbuilder.jar.
java -jar wljarbuilder.jar
you can test your app by running your web project from your IDE.
this is my netbeans project structure
8 Comments
Nanda
about 8 years agoholla win... gua nyampah dikit di blog elo deh...wkwkwk
Replyedwin
about 8 years agohahaa, jangan cuman nyampah. Kasih ide kek...
suhas
about 8 years agohi.. great blog. I m trying to do the same using OEPE. But I cant see the JNDI updated after I publish my EJB project on the server. EJB is deployed and shows under the Deployments table. Second thing, Is it ok, if I make web app and deploy it on same server and then access it without Apache ?? thanks
Replyedwin
about 8 years agohi suhas, thanks for your questions, 1. have you tried to exposed your EJB remote interfaces? If your EJBs are exposed, they should have a JNDI pattern like this --> MapedName#package.RemoteBeanName. 2. It's okay to deploy a webapp on a same VM or server with your EJB app. And you can exposed your EJB as a Local or Remote EJB. You can find a good article about when to use Local or Remote EJB here.
Tommy
about 5 years agoAre you still around? If so I have tried and tried this. I have tried many of the online examples. And I keep getting the same error: Uncaught [[o:Exception]:"java.lang.Exception: Invoke failed: [[o:InitialContext]]->lookup((o:String)[o:String]). Cause: javax.naming.NameNotFoundException: Name [weblogic.management.home] is not bound in this Context. Unable to find [weblogic.management.home]. VM: 1.7.0_09@http://java.oracle.com/" at Can you help?
Replyedwin
about 5 years agoYes Tommy, as you can seem the error happen because of "Name [weblogic.management.home]" perhaps your application cannot connect to your Context.PROVIDER_URL url try to ping or telnet to the destination Weblogic. :)
Eswar
about 5 years agoThanks Edwin ....Very useful tutorial ....
Replyedwin
about 5 years agoHi Eswar, glad it could help :)