obrigado pela dica Paulo
Olá a todos pessoal,
estou entrando agora na lista e já com uma dúvida…
Estou com um exemplo EJB Session Stateful que não consigo executar
Está dando a seguinte exceção:
javax.naming.NamingException: Error instantiating web-app JNDI-context: No location specified and no suitable instance of the type ‘cart_session.Cart’ found for the ejb-ref ejb/CartHome
Segue o código:
[code]-- BEAN
package cart_session;
import javax.ejb.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/* the bean class itself */
public class CartBean implements SessionBean {
SessionContext sessionContext;
java.lang.String _cardHolderName;
java.lang.String _creditCardNumber;
java.util.Date _expirationDate;
java.util.List _items = new ArrayList(10);
public void ejbCreate(String cardHolderName, String creditCardNumber, Date expirationDate) throws CreateException {
/**@todo Complete this method*/
this._cardHolderName = cardHolderName;
this._creditCardNumber = creditCardNumber;
this._expirationDate = expirationDate;
}
public void ejbRemove() {
/@todo Complete this method*/
}
public void ejbActivate() {
/@todo Complete this method*/
}
public void ejbPassivate() {
/@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public void addItem(Item item) {
/@todo Complete this method*/
System.out.println("\taddItem(" + item.getTitle() + "): " + this);
this._items.add(item);
}
public void removeItem(Item item) {
/**@todo Complete this method*/
System.out.println("\tremoveItem(" + item.getTitle() + "): " + this);
if (! _items.remove(item)) {
throw new EJBException("The item " + item.getTitle() + " is not in your cart.");
}
}
public java.util.List getContents() {
/**@todo Complete this method*/
System.out.println("\tgetContents(): " + this);
return _items;
}
public float getTotalPrice() {
/**@todo Complete this method*/
System.out.println("\tgetTotalPrice(): " + this);
float totalPrice = 0f;
for (int i = 0, n = _items.size(); i < n; i++) {
Item current = (Item) _items.get(i);
totalPrice += current.getPrice();
}
return ( (long) (totalPrice * 100)) / 100f;
}
public void purchase() {
/**@todo Complete this method*/
System.out.println("\tpurchase(): " + this);
Date today = new Date();
if (_expirationDate.before(today)) {
throw new EJBException("Expiration date: " + _expirationDate);
}
System.out.println("\tPurchasing not implemented yet!");
}
}
[/code]
--INTERFACE REMOTA
package cart_session;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
/* the bean?s REMOTE INTERFACE */
public interface Cart extends javax.ejb.EJBObject {
public void addItem(Item item) throws RemoteException;
public void removeItem(Item item) throws RemoteException;
public java.util.List getContents() throws RemoteException;
public float getTotalPrice() throws RemoteException;
public void purchase() throws RemoteException;
}
--INTERFACE HOME
package cart_session;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
/* the bean?s HOME INTERFACE */
public interface CartHome extends javax.ejb.EJBHome {
public Cart create(String cardHolderName, String creditCardNumber, Date expirationDate) throws CreateException, RemoteException;
}
--CHAMADA DO SERVLET
(...)
Object obj = null;
Context context=null;
String JNDIName = "java:comp/env/ejb/CartHome";
Hashtable env = new Hashtable();
env.put(Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
try {
// Create Initial Context and look up directory service of OC4J for object
context= new InitialContext( env );
obj = context.lookup(jndiName);
cartHome = (CartHome) PortableRemoteObject.narrow(obj, CartHome.class);
cartHome.create("TesteNome","99999999-89", new Date());
}catch (NamingException e) {
e.printStackTrace();
}
(...)
--WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>cartservlet</servlet-name>
<servlet-class>cart_session.CartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cartservlet</servlet-name>
<url-pattern>/cartservlet</url-pattern>
</servlet-mapping>
<ejb-ref>
<ejb-ref-name>ejb/CartHome</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>cart_session.CartHome</home>
<remote>cart_session.Cart</remote>
<!-- <ejb-link>cart_session</ejb-link> -->
</ejb-ref>
obs: estou usando
-Jbuilder 9
-OC4J (container)
obrigado
Zap
Editado para conter BBcode Code por jujo