Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Problem accessing EJB from JSP - page import syntax

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???!!!
 
author & internet detective
Posts: 40912
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<%@ 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.
 
Garry Jackson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic