Salve sto cercando di imparare l'utilizzo degli ejb 2.1 con jboss 6 ma continuo ad avere errori.
Scrivo il seguente codice dell'ejb:
codice:package prova.ejb; import java.rmi.RemoteException; import javax.ejb.EJBObject; public interface HelloWorld extends EJBObject { public String sayHello() throws RemoteException; }codice:package prova.ejb; import java.rmi.RemoteException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class HelloWorldBean implements SessionBean { /** * */ private static final long serialVersionUID = 1L; protected SessionContext ctx; // Methods of Remote interface public String sayHello() { return "Hello, world !"; } // Methods of Home interface public void ejbCreate() {} @Override public void ejbActivate() throws EJBException, RemoteException { // TODO Auto-generated method stub } @Override public void ejbPassivate() throws EJBException, RemoteException { // TODO Auto-generated method stub } @Override public void ejbRemove() throws EJBException, RemoteException { // TODO Auto-generated method stub } @Override public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException { // TODO Auto-generated method stub this.ctx = ctx; } }e il descrittore è il seguente:codice:package prova.ejb; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface HelloWorldHome extends EJBHome { public HelloWorld create() throws CreateException, RemoteException; }
quando vado a richiamare l'ejbcodice:<?xml version="1.0" encoding="UTF-8"?> <ejb-jar id="HelloWorld" version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"> <display-name>ProgettoDiProvaEJB</display-name> <enterprise-beans> <session> <description>HelloWorld deployment descriptor</description> <display-name>HelloWorld</display-name> <ejb-name>HelloWorld</ejb-name> <home>prova.ejb.HelloWorldHome</home> <remote>prova.ejb.HelloWorld</remote> <ejb-class>prova.ejb.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>HelloWorld</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>
ma ottengo sempre un errore di cast sulla HelloWorldHome. Mi sapere dire dove sto sbagliando?codice:package prova.ejb; import java.io.IOException; import java.io.PrintWriter; import javax.ejb.CreateException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Test extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Test() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = response.getWriter(); try { Context initialContext = new InitialContext(); Object objref = initialContext.lookup("HelloWorld"); //RIGA CHE VA IN ERRORE HelloWorldHome home = (HelloWorldHome)PortableRemoteObject.narrow(objref, HelloWorldHome.class); //RIGA CHE VA IN ERRORE HelloWorld myHelloWorld = home.create(); String message = myHelloWorld.sayHello(); out.println(message); } catch (Exception e) { System.err.println(" Erreur : " + e); System.exit(2); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
Grazie


Rispondi quotando