Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

servlet calling ejb in RI server  RSS feed

 
jay akhawri
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I am using j2sdkee1.3 and jsdk1.3. I want my servlet to call the bean.

i hope something like this will work..in my jsp but what about the generated
XYZClient.jar file, where should i keep it?

how will this work?...


<%@ page import="javax.naming.*"%>
<%@ page import="javax.rmi.PortableRemoteObject"%>
<%@ page import="java.util.Properties"%>
<%@ page import="com.nabil.ejb.Adder"%>
<%@ page import="com.nabil.ejb.AdderHome"%>

<%
// preparing a Properties object for constructing
// an initial context
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.enterprise.naming.SerialInitContextFactory");
properties.put(Context.PROVIDER_URL, "iiop://localhost:1050");

try {
// Get an initial context
InitialContext jndiContext = new InitialContext(properties);
System.out.println("Got context");
// Get a reference to the Bean
Object ref = jndiContext.lookup("Adder");
System.out.println("Got reference");

// Get a reference from this to the Bean's Home interface
AdderHome home = (AdderHome)
PortableRemoteObject.narrow (ref, AdderHome.class);

// Create an Adder object from the Home interface
Adder adder = home.create();
out.println ("2 + 5 = " + adder.add(2, 5));
}
catch(Exception e) {
System.out.println(e.toString());
}
%>
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jay,

There are couple of things here that might improve your code:
  • You don�t need to set the jndi properties in order to create the initial context, because the ejb runs within the server environment and all these properties are already set. In that case the InitialContext jndiContext = new InitialContext(); should be enough.
  • If the web server & the ejb container run within the same jvm you might try to use local interfaces.
  • Try to use ejb references.


  • All this you might try later on in order to improve your prototype.
    Back to your question: in order for your servlet to work you need to make the com.nabil.ejb.Adder and com.nabil.ejb.AdderHome classes available to your web application. The simplest way to do this is to pack those two classes in a client jar file and copy it to the WEB-INF/lib folder within the war.
    By the way did you get any error messages? If that�s the case, maybe you need to post the error stack trace as well.
    Regards.
     
    jay akhawri
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi valentin,

    sorry for not posting the errors....there are errors but i felt like knowing the right steps wud help me understand where these errors are coming from. Besides when i was posting the code it was just a concept.

    another stupid question..u have mentioned two classes...
    >> you need to make the com.nabil.ejb.Adder and com.nabil.ejb.AdderHome
    >> classes available to your web application

    do i have to extend my client code to them or they are already available in some form of jar files?.

    thanx
    have a good day
    Jay
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!