sanjeev_1772
asked on
trying to call an ejb from a servlet-client
sir,
I am just a biginner in weblogic and I am facing a problem here and I think you will provide a nice solution to me .
Environment : J2EE/weblogic 5.1/ejb1.1/servlets/win'20 00
I have successfuly deployed a stateless session bean named as "Game" and the jndi name used is "gum" at Weblogic 5.1.
Elements inside the "Game.jar" are :
remote interface-> Game.class
home interface -> GameHome.class
bean class -> GameBean.class
xml DD -> META-INF/ejb-jar.xml
I have maintained the dir. structure as follows : WEB-INF/lib /Game.jar
WEB-INF/classes/toy/*.clas s
WEB-INF/classes/toy/META-I NF/ejb-jar .xml
and using a client servlet class "GameServ.class"
WEB-INF/classes/toy/GameSe rv.class
Now till I think everything is all right...
Now i started the weblogic...then called the index page
and from the index page I am calling the bean using the mentioned servlet client "GameServ.class" and during calling process i am getting a exception "java.lang.ClassCastExcept ion" at the line mentioned below inside ClentServ.class..>
line of error-->"GameHome sh=(GameHome) PortableRemoteObject.narro w(ref,Game Home.class );"
I tried my best but i could not get out of this problem and for more clearity i am writing the java files inside a package "toy" used here..as below....>>
******************* files used *****************
***** remote interface *******
package toy;
import java.rmi.*;
import javax.ejb.*;
public interface Game extends EJBObject
{
public String getName()throws RemoteException;
public int getSum(int x1,int x2) throws RemoteException;
}
********** home interface *********
package toy;
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
public interface GameHome extends EJBHome
{
public Game create()throws RemoteException,CreateExce ption;
}
******** bean **********
package toy;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContex t;
import javax.sql.DataSource;
import javax.naming.NamingExcepti on;
import javax.ejb.EJBException;
import java.sql.*;
public class GameBean implements SessionBean
{
public void ejbActivate() {System.out.println("Activ ate"); }
public void ejbRemove() { System.out.println("remove ");}
public void ejbPassivate() { System.out.println("<..... passivate. ...>"); }
public void setSessionContext(SessionC ontext ctx) { System.out.println("<....s essioncont ext....>") ; }
public void ejbCreate () throws CreateException { System.out.println("<..... create.... >"); }
public String getName()
{
System.out.println("<..... chk 1 .....>");
String name="GAME";
return name;
}
public int getSum(int x1,int x2)
{
int x;
x=x1+x2;
System.out.println("<..... chk 5 ...x..> "+x);
return x;
}
}
**************** ejb-jar.xml **************
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>Game</ejb-name>
<home>toy.GameHome</home>
<remote>toy.Game</remote>
<ejb-class>toy.GameBean</e jb-class>
<session-type>Stateless</s ession-typ e>
<transaction-type>Containe r</transac tion-type>
</session>
</enterprise-beans>
</ejb-jar>
************** clent servlet ***************
package toy;
import toy.Game;
import toy.GameHome;
import javax.ejb.*;
import javax.rmi.*;
import javax.rmi.PortableRemoteOb ject;
import javax.naming.InitialContex t;
import javax.naming.Context;
import javax.naming.*;
import java.io.*;
import java.util.Properties;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GameServ extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
res.setContentType("text/h tml");
PrintWriter out = res.getWriter();
int ss=0;
String name="";
System.out.println("inside client : GameServ ");
try
{
String url = "t3://localhost:7001";
Properties h = new Properties();
h.put(Context.INITIAL_CONT EXT_FACTOR Y,"weblogi c.jndi.WLI nitialCont extFactory ");
h.put(Context.PROVIDER_URL , url);
Context ctx = new InitialContext(h);
Object ref = ctx.lookup("gum");
System.out.println("inside client : objref-3 "+ref);
System.out.println("....in side Zone...");
(**) GameHome sh=(GameHome) PortableRemoteObject.narro w(ref,Game Home.class );
//here i am getting >> ClassCastEXCEPTION<<
System.out.println("inside client : sh-4 "+sh);
Game sr =sh.create();
System.out.println("inside client : sr-5 "+sr);
name=sr.getName();
System.out.println(" Client ...Got the name : "+name);
ss = sr.getSum(1,7);
System.out.println("Got the Sum :" + String.valueOf(ss));
}
catch(Exception e)
{
System.out.println(e);
}
out.println("<html><head>\ n" +
"<title>Check Servlet</title>\n" +
"</head>\n" +
"<body bgcolor=#ffffff> <font face=\"Comic Sans MS\" size+=4 color=red>" +
"<br> <center><h1> name = "+name+"</h1></center>" +
"<br> <center><h1> sum = "+ss+"</h1></center>" +
"<center><h1>Game</h1></ce nter>");
out.println("</body></html >");
}
}
************************** Exception I am getting ************************** **
inside the "toy.GameServ.java" i am getting >> ClassCastEXCEPTION<< at line (**) as below
" GameHome sh=(GameHome) PortableRemoteObject.narro w(ref,Game Home.class ); "
I hope i have described my problem very well and i request you to give me solution so that my client application may call the bean.
******** Waiting for the response. *******
************************** ***** END ************************** ********** ****
I am just a biginner in weblogic and I am facing a problem here and I think you will provide a nice solution to me .
Environment : J2EE/weblogic 5.1/ejb1.1/servlets/win'20
I have successfuly deployed a stateless session bean named as "Game" and the jndi name used is "gum" at Weblogic 5.1.
Elements inside the "Game.jar" are :
remote interface-> Game.class
home interface -> GameHome.class
bean class -> GameBean.class
xml DD -> META-INF/ejb-jar.xml
I have maintained the dir. structure as follows : WEB-INF/lib /Game.jar
WEB-INF/classes/toy/*.clas
WEB-INF/classes/toy/META-I
and using a client servlet class "GameServ.class"
WEB-INF/classes/toy/GameSe
Now till I think everything is all right...
Now i started the weblogic...then called the index page
and from the index page I am calling the bean using the mentioned servlet client "GameServ.class" and during calling process i am getting a exception "java.lang.ClassCastExcept
line of error-->"GameHome sh=(GameHome) PortableRemoteObject.narro
I tried my best but i could not get out of this problem and for more clearity i am writing the java files inside a package "toy" used here..as below....>>
******************* files used *****************
***** remote interface *******
package toy;
import java.rmi.*;
import javax.ejb.*;
public interface Game extends EJBObject
{
public String getName()throws RemoteException;
public int getSum(int x1,int x2) throws RemoteException;
}
********** home interface *********
package toy;
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
public interface GameHome extends EJBHome
{
public Game create()throws RemoteException,CreateExce
}
******** bean **********
package toy;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContex
import javax.sql.DataSource;
import javax.naming.NamingExcepti
import javax.ejb.EJBException;
import java.sql.*;
public class GameBean implements SessionBean
{
public void ejbActivate() {System.out.println("Activ
public void ejbRemove() { System.out.println("remove
public void ejbPassivate() { System.out.println("<.....
public void setSessionContext(SessionC
public void ejbCreate () throws CreateException { System.out.println("<.....
public String getName()
{
System.out.println("<.....
String name="GAME";
return name;
}
public int getSum(int x1,int x2)
{
int x;
x=x1+x2;
System.out.println("<.....
return x;
}
}
**************** ejb-jar.xml **************
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>Game</ejb-name>
<home>toy.GameHome</home>
<remote>toy.Game</remote>
<ejb-class>toy.GameBean</e
<session-type>Stateless</s
<transaction-type>Containe
</session>
</enterprise-beans>
</ejb-jar>
************** clent servlet ***************
package toy;
import toy.Game;
import toy.GameHome;
import javax.ejb.*;
import javax.rmi.*;
import javax.rmi.PortableRemoteOb
import javax.naming.InitialContex
import javax.naming.Context;
import javax.naming.*;
import java.io.*;
import java.util.Properties;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GameServ extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void service(HttpServletRequest
throws IOException
{
res.setContentType("text/h
PrintWriter out = res.getWriter();
int ss=0;
String name="";
System.out.println("inside
try
{
String url = "t3://localhost:7001";
Properties h = new Properties();
h.put(Context.INITIAL_CONT
h.put(Context.PROVIDER_URL
Context ctx = new InitialContext(h);
Object ref = ctx.lookup("gum");
System.out.println("inside
System.out.println("....in
(**) GameHome sh=(GameHome) PortableRemoteObject.narro
//here i am getting >> ClassCastEXCEPTION<<
System.out.println("inside
Game sr =sh.create();
System.out.println("inside
name=sr.getName();
System.out.println(" Client ...Got the name : "+name);
ss = sr.getSum(1,7);
System.out.println("Got the Sum :" + String.valueOf(ss));
}
catch(Exception e)
{
System.out.println(e);
}
out.println("<html><head>\
"<title>Check Servlet</title>\n" +
"</head>\n" +
"<body bgcolor=#ffffff> <font face=\"Comic Sans MS\" size+=4 color=red>" +
"<br> <center><h1> name = "+name+"</h1></center>" +
"<br> <center><h1> sum = "+ss+"</h1></center>" +
"<center><h1>Game</h1></ce
out.println("</body></html
}
}
**************************
inside the "toy.GameServ.java" i am getting >> ClassCastEXCEPTION<< at line (**) as below
" GameHome sh=(GameHome) PortableRemoteObject.narro
I hope i have described my problem very well and i request you to give me solution so that my client application may call the bean.
******** Waiting for the response. *******
**************************
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.