Jsp cannot find EJB package

Hello-

I have built a simple CMP entity EJB and tried to use it in a jsp. However, I keep getting a compile error that the package does not exist. I have tried putting the jar file in several locations and adding them all to my classpath in the application server admin console. I have even added the specific JAR to the classpath. No matter what-no luck.

What do I need to do to get this package recognized? Is there a good tutorial out there for building and deploying EJB's? I have gone through every one I can find and have not found any mention of moving a JAR file to a certain location so it will be recognized.

Thanks for your help

[668 byte] By [PerryAa] at [2007-11-26 13:10:00]
# 1

EJBs are accessed from web-clients by locating the home interface, creating an enterprise bean instance, and invoking a business method.

Ref: http://www.netbeans.org/kb/50/quickstart-j2ee.html

The above tutorial provides a quick start guide for developing an enterprise app in NetBeans 5.0.

- If you are using jse8.1( based on NetBeans 5.0) or nb5, then the above tutorial should work as-is. If you are using jse8.1 (based on NetBeans 4.1), then some instructions may vary.

- The tutorial uses a servlet for accessing ejb. The concept should apply for jsps too.

Also:

1. http://java.sun.com/j2ee/1.4/docs/tutorial/doc/EJB5.html#wp79985

J2EE Tutorial for j2ee applications.

2. The same tutorial modified for use with NetBeans/JSE ides:

http://www.netbeans.org/download/docs/41/j2ee-tutorial/EJB.html#wp80471

KarthikRa at 2007-7-7 17:23:34 > top of Java-index,Development Tools,Java Tools...
# 2

Thanks for the feedback. My experience( using IBM Websphere) is that you can import EJB packages and instantiate the EJB's like any other object right on the jsp:

<%page import="com.hs.ejbs.*"%>

EJBObject ejb = new EJBObject();

It appears that the Sun App server requires no package import, but a "lookup" instead?

Object objRef = ic.lookup("java:comp/env/ejb/TheEJB");

or

return ((ejb.CustomerFacadeRemoteHome) getServiceLocator().getRemoteHome("java:comp/env/ejb/CustomerFacadeBean",ejb.Cu stomerFacadeRemoteHome.class)).create();

The first one looks much easier. Why are there two different ways? What is the difference?

If there is no package to import, where does the container get the class files from?

Do I need to import packages in order to do the lookup?

Again, any advice on this whole EJB instantiation paradigm is greatly appreciated!

PerryAa at 2007-7-7 17:23:34 > top of Java-index,Development Tools,Java Tools...
# 3

I still seem to be having the same problem. I tried putting some quick & dirty java code directly into the jsp and by pass the session bean and servlet. I keep getting compile errors that the package is not there:

testEJB_jsp.java:8: package com.hs.ejbs does not exist import com.hs.ejbs.*;

testEJB_jsp.java:66: package com.hs.ejbs does not exist

com.hs.ejbs.XreviewsLocalHome rv;

^

testEJB_jsp.java:73: package com.hs.ejbs does not exist

rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("java:comp/env/ejb/XreviewsBean");

Here is my code:

<%@page contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<%@page import= "javax.ejb.*" %>

<%@page import= "javax.naming.*" %>

<%@page import= "com.hs.ejbs.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>testEJB</title>

</head>

<body>

<%

SessionContext context;

com.hs.ejbs.XreviewsLocalHome rv;

try {

javax.naming.Context c = new javax.naming.InitialContext();

rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("java:comp/env/ejb/XreviewsBean");

} catch(Exception e){

out.println("ejb retrieval error e="+e);

}

out.println("made it past the ejb retrieval code");

%>

</body>

</html>

PerryAa at 2007-7-7 17:23:34 > top of Java-index,Development Tools,Java Tools...
# 4

> I tried putting some quick & dirty java code directly into the jsp

> and by pass the session bean and servlet.

I could be wrong but i don't think it is possible to bypass the bean. EJBs are actually created by an ejb container in the server and the server provides the lifecycle services for the beans. A web client (jsp or servlet) on the other hand runs in the server's web container and should interact with an ejb only via the j2ee specified interfaces.

Regarding compilation, the class com.hs.ejbs.XreviewsLocalHome, for instance, implements several interfaces defined in j2ee apis; so even if the com.hs.ejbs jar is put in the classpath, the compilation will fail since it will not find the base j2ee apis that hs.ejbs itself depends on...

KarthikRa at 2007-7-7 17:23:34 > top of Java-index,Development Tools,Java Tools...
# 5

So I tried to follow the tutorial without shortcuts using my own database and now I get a different issue.Here is what I see:

ERROR:

-

Invalid jndi name [] found in application ejb [XreviewsSessionBean];

sun-ejb.jar.xml:

--

<sun-ejb-jar>

<enterprise-beans>

<cmp-resource>

<jndi-name>jdbc/hspool</jndi-name>

</cmp-resource>

<ejb>

<ejb-name>XreviewsBean</ejb-name>

<jndi-name>XreviewsEJB</jndi-name>

</ejb>

</enterprise-beans>

</sun-ejb-jar>

methods tried (and failed) in XreviewsSessionBean. I am assuming this is where the error is, since it is the only apparent database call in the code for this bean:

-

1) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("java:comp/env/ejb/XreviewsBean");

2) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("jdbc/hspool/XreviewsBean");

3) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("jdbc/hspool/ejb/XreviewsBean");

4) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("jdbc/hspool/XreviewsEJB");

5) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("jdbc/hspool/com.hs.ejbs.XreviewsEJB");

6) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("hspoolPM/ejb/XreviewsBean");

7) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("jdbc/hspoolPM/ejb/XreviewsBean");

8) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("XreviewsEJB");

9) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("jdbc/hspool/ejb/XreviewsEJB");

10) com.hs.ejbs.XreviewsLocalHome rv = (com.hs.ejbs.XreviewsLocalHome) c.lookup("java:comp/env/ejb/XreviewsEJB");

I have the following set up in the server itself:

JDBC Resource - jdbc/hspool

Connection pool - hspool

persistence manager - hspoolPM

Any idea why the jndi name is erroneous?

PerryAa at 2007-7-7 17:23:34 > top of Java-index,Development Tools,Java Tools...
# 6
Also followed up in: http://forum.java.sun.com/thread.jspa?threadID=734824According to the above thread, the problem was solved by modifying ejb-jar.xml, by adding <jndi-name> tag to the <entity> tag,,,
KarthikRa at 2007-7-7 17:23:34 > top of Java-index,Development Tools,Java Tools...