Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Can't call ejb from Servlet

  Asked By: Anpu    Date: Dec 28    Category: Java    Views: 1633
  

I m requesting for ur most precious time to go through it ,
and waiting 4 replay
I m creating an ejb , and trying to invoke from client
I created it, While Invoking from client It shows som error msg like this
server side output
------------------
LOOKING UP HOME.................
INSIDE LOOKUP HOME ............
getting initialContext........
The client was unable to lookup the EJBHome. Please make sure
that you have deployed the ejb with the JNDI name HelloWorld on the WebLogic ser
ver at http://localhost:5678
Exception here........................javax.naming.NameNotFoundException: Unable
to resolve HelloWorld. Resolved: '' Unresolved:'HelloWorld' ; remaining name ''
Client side output
------------------
Naming Exception
Exception

note
----
JNDI_NAME = "HelloWorld"; file://(my jar file name)
url = "http://localhost:5678";// I am getting the servlet which invoke ejb by
this url
my jar is HelloWorld.jar
contents of this mail as follows
--------------------------------
1.Directory structure & IDE
2.Program calling ejb (ClientServlet1.java)
3.ServerSide msg(ouput)
4.ClientSide msg(ouput)
5.Hello.class
6.HelloBean.class
7.HelloClient.class
8.HelloHome.class
9.HelloLocal.class
10.HelloLocalHome.class
11.ejb-jar.xml
12.HelloWorld.jar
directory structure
-------------------
D:\abc\config\sumaria\applications\DefaultWebApp\WEB-INF\classes\examples\
IDE - NetBeans 3.6
App Server - weblogic
Program calling ejb
----------------
package examples;
import examples.*;
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import java.io.*;
public class ClientServlet1 extends HttpServlet{
private static final String JNDI_NAME = "HelloWorld";
private String url;
private HelloHome home;
private Hello remot;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException {
doPost(req,res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
PrintWriter out = res.getWriter();

try{
logger(" LOOKING UP HOME.................");
url = "http://localhost:5678";
home = lookupHome(out);
remot = (Hello) narrow(home.create(),Hello.class);
logger(" Finished getting home interface......");
String myint = remot.hello();
out.println("<h2> MYINTffffffffffff </h2> <h1> "+myint+"</h1>");
}
catch(Exception ex) {
out.println("Exception<br>");
logger("Exception here........................"+ex);
}
}
protected String example(HelloHome hom)
throws CreateException, RemoteException, RemoveException {
logger("inside Example...................");
String s= remot.hello();
return s;
}
protected Object narrow(Object ref, Class c) {
logger("Inside object narrow.....");
return PortableRemoteObject.narrow(ref, c);
}
protected HelloHome lookupHome(PrintWriter out) throws NamingException {
// Lookup the beans home using JNDI
logger(" INSIDE LOOKUP HOME ............");
Context ctx = getInitialContext(out);
try {
Object home = ctx.lookup(JNDI_NAME);
logger(" Returning Home Interface..................");
return (HelloHome) narrow(home, HelloHome.class);

} catch (NamingException ne) {
out.println("Naming Exception<br>");
logger("The client was unable to lookup the EJBHome. Please make sure ");
logger("that you have deployed the ejb with the JNDI name "+
JNDI_NAME+" on the WebLogic server at "+url);
throw ne;
}
}
protected Context getInitialContext(PrintWriter out) throws NamingException {
try {
// Get an InitialContext
Hashtable h = new Hashtable();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
logger("getting initialContext........");
return new InitialContext(h);
} catch (NamingException ne) {
out.println("NamingException<br>");
logger("We were unable to get a connection to the WebLogic server at
"+url);
logger("Please make sure that the server is running.");
throw ne;
}
}
protected void logger(String s) { System.out.println(s); }
}

server side output
------------------
LOOKING UP HOME.................
INSIDE LOOKUP HOME ............
getting initialContext........
The client was unable to lookup the EJBHome. Please make sure
that you have deployed the ejb with the JNDI name HelloWorld on the WebLogic ser
ver at http://localhost:5678
Exception here........................javax.naming.NameNotFoundException: Unable
to resolve HelloWorld. Resolved: '' Unresolved:'HelloWorld' ; remaining name ''
Client side output
------------------
Naming Exception
Exception

--------------------------------------------------------------------------------\
-------
Hello.java
----------
package examples;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface Hello extends javax.ejb.EJBObject

{
public String hello() throws
java.rmi.RemoteException;
}
HelloBean.java
---------------------
package examples;
import javax.ejb.*;
public class HelloBean implements
javax.ejb.SessionBean {
private SessionContext ctx;

public void ejbCreate()
{
System.out.println("ejbCreate()");
}

public void ejbRemove()
{
System.out.println("ejbRemove()");
}
public void ejbActivate()
{
System.out.println("ejbActivate()");
}
public void ejbPassivate()
{
System.out.println("ejbPassivate()");
}
public void
setSessionContext(javax.ejb.SessionContext ctx)
{
this.ctx = ctx;
}
public String hello()
{
System.out.println("hello()");
return "Hello World";
}
}
HelloClient.java
----------------
package examples;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;
public class HelloClient
{
public static void main(String[] args) throws
Exception
{ Properties props = System.getProperties();
Context ctx = new InitialContext(props);
Object obj = ctx.lookup("HelloHome");
HelloHome home =

(HelloHome)javax.rmi.PortableRemoteObject.narrow(obj,
HelloHome.class);
Hello hello = home.create();
System.out.println(hello.hello());
hello.remove();
}
}
HelloHome.java
---------------
package examples;
public interface HelloHome extends
javax.ejb.EJBHome
{
Hello create() throws
java.rmi.RemoteException,javax.ejb.CreateException;
}
HelloLocal.java
----------------
package examples;
public interface HelloLocal extends
javax.ejb.EJBLocalObject
{
public String hello();
}
HelloLocalHome.java
-------------------
package examples;
public interface HelloLocalHome extends
javax.ejb.EJBLocalHome
{
HelloLocal create() throws
javax.ejb.CreateException;
}
ejb-jar.xml
------------
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : web.xml
Created on : July 4, 2004, 7:13 AM
Author : Administrator
Description:
Purpose of the document follows.
-->
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>Hello</ejb-name>
<home>examples.HelloHome</home>
<remote>examples.Hello</remote>

<local-home>examples.HelloLocalHome</local-home>
<local>examples.HelloLocal</local>
<ejb-class>examples.HelloBean</ejb-class>
<session-type>Stateless</session-type>

<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
HelloWorld.jar

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Can't call ejb from Servlet Or get search suggestion and latest updates.




Tagged: