File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Problem accessing EJB from JSP - page import syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Problem accessing EJB from JSP - page import syntax" Watch "Problem accessing EJB from JSP - page import syntax" New topic
Author

Problem accessing EJB from JSP - page import syntax

Garry Jackson
Greenhorn

Joined: Aug 11, 2003
Posts: 8
Hi, I am a little stumped...
I have deployed a test EJB inside a war to JBoss. JBoss says that it has deployed the package, Hello.war
This war has the following structure:
..\
HelloBean.class
HelloHome.class
HelloObject.class
test.jsp
meta-inf\
ejb-jar.xml
manifest.mf
Now I am not conforming to the MVC standards by implementing a call to the EJB from within "test.jsp", but I know you can do it, so I need to test. My jsp file has the following code:
<%@ page import="HelloHome.*, javax.naming.InitialContext,javax.naming.Context"%>
<html> .. etc. etc and then the java piece
<%
Context initCtx = new InitialContext();
try {
Object object = initCtx.lookup("java:comp/env/ejb/hello");
HelloHome helloHome = (HelloHome)
javax.rmi.PortableRemoteObject.narrow(object, HelloHome.class);
HelloObject bean = helloHome.create();
} catch (Exception e) {
%> There has been an error <%
}
%>
<%= bean.sayHello() %>

When I load the page from the browser, JBoss goes wild and I get the following response
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: -1 in the jsp file: null
Generated servlet error:
[javac] Compiling 1 source file
[javac] C:\jboss-3.2.1_tomcat-4.1.24\server\default\tmp\deploy\server\default\deploy\work\MainEngine\localhost\Hello\test_jsp.java:7: package HelloHome does not exist
[javac] import HelloHome.*;
[javac] ^
[javac] C:\jboss-3.2.1_tomcat-4.1.24\server\default\tmp\deploy\server\default\deploy\work\MainEngine\localhost\Hello\test_jsp.java:54: cannot resolve symbol
[javac] symbol : class HelloHome
[javac] location: class org.apache.jsp.test_jsp
[javac] HelloHome helloHome = (HelloHome)
WHAT AM I DOING WRONG ???!!!
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31069
    
232

<%@ page import="HelloHome.*, javax.naming.InitialContext,javax.naming.Context"%>

This means that you are trying to import all of the classes in the HelloHome package. You would need to use import="HelloHome,javax... to just import the HelloHome class.
Also, you should consider putting all the classes in a package instead of using the default package because some app servers get confused about the default package.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Garry Jackson
Greenhorn

Joined: Aug 11, 2003
Posts: 8
Hi
I generated the page context as suggested but got this error:
Generated servlet error:
[javac] Compiling 1 source file
[javac] C:\jboss-3.2.1_tomcat-4.1.24\server\default\tmp\deploy\server\default\deploy\work\MainEngine\localhost\Hello\test_jsp.java:7: '.' expected
[javac] import HelloHome;
[javac] ^

I think you are right, I need to package everything properly and somehow set the "context" on the JBoss server. Don't know what else may help....
Garry Jackson
Greenhorn

Joined: Aug 11, 2003
Posts: 8
I have managed to package the classes into a "comms" package, so this problem has moved on somewhat. Now I get this....
Testing connection through to the bean
Setting object context
javax.naming.NameNotFoundException: xxx not bound
No matter how I call it, either like this:
Object obj = initCtx.lookup("java:comp/env/xxx")
or
Object obj = initCtx.lookup("xxx");
My ejb-jar.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC
"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
"http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>xxx</ejb-name>
<jndi-name>xxx</jndi-name>
<home>comms.HelloHome</home>
<remote>comms.HelloObject</remote>
<ejb-class>comms.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>xxx</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem accessing EJB from JSP - page import syntax