Unable to connect to EJB bean from servlet, but I can from s
arrissol Sep 23, 2001 3:43 AM
I am running tomcat and JBoss separately. And I
would
like to access an ejb from a servlet just as my
client
application did.
I tried the ejb interest example and it worked just
fine.
The client application was able to communicated with
a
remote ejb. I modified this client application into
a
servlet and complied it just fine.
During execution, the servlet throws the exception
below. Sure JBoss was running as I did successfully
run the usual client application.
Then copied the jbosssx-client.jar,jboss-client.jar,
jnp-client.jar and even the jaas.jar and ejb.jar
files
into tomcats lib directory, but the error persisted
(even after restarting tomcat).
I have learnt one thing though, the line
"Got Context" is been executed then the exception gets
thrown as I never get to see the line "Got reference".
I think the problem could be in the way I have set the
environmental variables.
Error javax.naming.NoInitialContextException: Cannot
instantiate class:
org.jnp.interfaces.NamingContextFactory [Root
exception is java.lang.ClassNotFoundException:
org.jnp.interfaces.NamingContextFactory]
The servlet looks like this.
package com.burudani.interest; import java.util.Properties; import javax.rmi.PortableRemoteObject; import javax.naming.*; import com.burudani.interest.Interest; import com.burudani.interest.InterestHome; import java.io.*; import java.text.*; import java.util.*; import javax.servlet.http.*; import javax.servlet.*; public class InterestClientServlet extends HttpServlet { private void executeServer(String JavaNamingProviderURLValue,String JndiContextLookupValue,PrintWriter out) { out.println("inside executeServer()"); String JavaNamingProviderURL="localhost:1099"; String JndiContextLookup="Interest"; Date ServerDate=new Date(); SimpleDateFormat sdf; sdf=new SimpleDateFormat("dd MMM yyyy hh:mm:ss:ms zzz"); try { /*System.out.println("Set the \"java.naming.provider.url\""); BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); String str="";int i; for (i=0;i<1;i++) str=br.readLine(); JavaNamingProviderURL=str; System.out.println("Enter the lookup jndi Name of remote object"); br=new BufferedReader (new InputStreamReader(System.in)); for (i=0;i<1;i++) str=br.readLine(); */ JavaNamingProviderURL=JavaNamingProviderURLValue; JndiContextLookup=JndiContextLookupValue; out.println("Setting the \"java.naming.provider.url\""); out.println(JavaNamingProviderURL); out.println("Setting the lookup jndi Name of remote object"); out.println(JndiContextLookup); } catch(Exception ioe) { System.out.println("Problems reading your values "+ioe.toString()); } //set up the naming provider, this may not be necessary, depending on how your Java system is configured. Properties env=new Properties(); env.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); //env.setProperty("java.naming.provider.url","localhost:1099"); env.setProperty("java.naming.provider.url",JavaNamingProviderURL); env.setProperty("java.naming.factory.url.pkgs","org.jboss.naming"); try { InitialContext jndiContext=new InitialContext(env); out.println("Got context"); //Object ref=jndiContext.lookup("Interest"); Object ref=jndiContext.lookup(JndiContextLookup); out.println("Got Reference"); //Get a reference from this to the Bean's Home interface. InterestHome home=(InterestHome)PortableRemoteObject.narrow(ref,InterestHome.class); //Create an Interest Object using the Home Interface Interest interest=home.create(); ServerDate=interest.getServerDate(); System.out.println("Server date is "+sdf.format(ServerDate)); //Now call the calculateCompoundInterest() method to do the interest calculation. System.out.println("Interest on 1000 units, at 10% per period, compounded over 2 periods is: "); System.out.println(interest.calculateCompoundInterest(1000,0.10,2)); ServerDate=interest.getServerDate(); System.out.println("Server date is "+sdf.format(ServerDate)); } catch(Exception re){System.out.println("Error "+re.toString());} /*catch(javax.naming.NamingException ne){System.out.println("NamingException Error "+ne.toString());} catch(java.ejb.CreateException ce){System.out.println("java.ejb.CreateException Error "+ce;} catch(java.ejb.FinderException fe){System.out.println("java.ejb.FinderException Error "+fe;} */ } public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { PrintWriter out=res.getWriter(); out.println("there"); executeServer("132.107.151.36","ejb/Interest",out); //just hardcode values for testing purposes. } }